如何解决TypeError:无法将undefined或null转换为object [英] How to resolve TypeError: Cannot convert undefined or null to object
问题描述
我编写了几个有效复制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 作为参数的函数但是传递 undefined 时会导致此错误或者 null ,例如
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
。因此,如果将实际的 null
值传递给脚本,它将在代码的Object部分中进行解析。并且 Object.keys(null)
抛出提到的 TypeError
。要修复它,请使用 if(obj === null){return null}
- 没有qoutes null。
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转换为object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!