如何解决 TypeError:无法将 undefined 或 null 转换为对象 [英] How to resolve TypeError: Cannot convert undefined or null to object

查看:264
本文介绍了如何解决 TypeError:无法将 undefined 或 null 转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了几个有效复制 JSON.stringify() 的函数,将一系列值转换为字符串化版本.当我将代码移植到 JSBin 并在一些示例值上运行它时,它运行得很好.但是我在一个旨在测试这个的规范运行器中遇到了这个错误.

I've written a couple of functions that effectively replicate JSON.stringify(), converting a range of values into stringified versions. When I port my code over to JSBin and run it on some sample values, it functions just fine. But I'm getting this error in a spec runner designed to test this.

我的代码:

  // five lines of comments
  var stringify = function(obj) {
  if (typeof obj === 'function') { return undefined;}  // return undefined for function
  if (typeof obj === 'undefined') { return undefined;} // return undefined for undefined
  if (typeof obj === 'number') { return obj;} // number unchanged
  if (obj === 'null') { return null;} // null unchanged
  if (typeof obj === 'boolean') { return obj;} // boolean unchanged
  if (typeof obj === 'string') { return '"' + obj + '"';} // string gets escaped end-quotes
  if (Array.isArray(obj)) { 
    return obj.map(function (e) {  // uses map() to create new array with stringified elements
        return stringify(e);
    });
  } else {
    var keys = Object.keys(obj);   // convert object's keys into an array
    var container = keys.map(function (k) {  // uses map() to create an array of key:(stringified)value pairs
        return k + ': ' + stringify(obj[k]);
    });
    return '{' + container.join(', ') + '}'; // returns assembled object with curly brackets
  }
};

var stringifyJSON = function(obj) {
    if (typeof stringify(obj) != 'undefined') {
        return "" + stringify(obj) + "";
    }
};

我从测试人员那里得到的错误信息是:

The error message I'm getting from the tester is:

TypeError: Cannot convert undefined or null to object
    at Function.keys (native)
    at stringify (stringifyJSON.js:18:22)
    at stringifyJSON (stringifyJSON.js:27:13)
    at stringifyJSONSpec.js:7:20
    at Array.forEach (native)
    at Context.<anonymous> (stringifyJSONSpec.js:5:26)
    at Test.Runnable.run (mocha.js:4039:32)
    at Runner.runTest (mocha.js:4404:10)
    at mocha.js:4450:12
    at next (mocha.js:4330:14)

它似乎失败了:stringifyJSON(null) 例如

It seems to fail with: stringifyJSON(null) for example

推荐答案

通用答案

当您调用一个期望 Object 作为其参数的函数,但传递 undefinednull 时,会导致此错误,例如 for例子

This error is caused when you call a function that expects an Object as its argument, but pass undefined or null instead, like for example

Object.keys(null)
Object.assign(window.UndefinedVariable, {})

由于这通常是错误的,解决方案是检查您的代码并修复 null/undefined 条件,以便函数要么获得正确的 Object,要么根本不会被调用.

As that is usually by mistake, the solution is to check your code and fix the null/undefined condition so that the function either gets a proper Object, or does not get called at all.

Object.keys({'key': 'value'})
if (window.UndefinedVariable) {
    Object.assign(window.UndefinedVariable, {})
}

针对相关代码的答案

if (obj === 'null') { return null;}//null 不变 不会仅在给定字符串 "null" 时评估当给定 null.因此,如果您将实际的 null 值传递给您的脚本,它将在代码的 Object 部分进行解析.而 Object.keys(null) 会抛出提到的 TypeError.要修复它,请使用 if(obj === null) {return null} - 没有围绕 null 的 qoutes.

The line if (obj === 'null') { return null;} // null unchanged will not evaluate when given null, only if given the string "null". So if you pass the actual null value to your script, it will be parsed in the Object part of the code. And Object.keys(null) throws the TypeError mentioned. To fix it, use if(obj === null) {return null} - without the qoutes around null.

这篇关于如何解决 TypeError:无法将 undefined 或 null 转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆