无法将未定义或空转换为对象错误
2017-10-29
2156
此处的代码我最近无法启动,因为出现错误,无法将未定义或空值转换为对象:
Utils.getCardsInSets((ERR, DATA) => {
if (!ERR) {
allCards = DATA;
console.log("Card data loaded. [" + Object.keys(DATA).length + "]");
} else {
console.log("An error occurred while getting cards: " + ERR);
}
});
控制台上显示的错误
console.log("Card data loaded. [" + Object.keys(DATA).length + "]");
^
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at Utils.getCardsInSets (/root/test/index.js:37:52)
at Request.request [as _callback] (/root/test/utils.js:41:13)
at Request.self.callback (/root/test/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/root/test/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/root/test/node_modules/request/request.js:1085:12)
1个回答
您的
Utils.getCardsInSets
返回
undefined
或
null
DATA
。在上面的代码中,您没有向该方法传递任何查询参数,请检查是否需要传递任何参数。
如果所做的一切都是正确的,则方法将根据特定条件返回
undefined/null
。只需检查
DATA
,然后获取其键长度即可。
Utils.getCardsInSets((ERR, DATA) => {
if (!ERR) {
allCards = DATA;
var datalength = (!!DATA) ? Object.keys(DATA).length : 0;
console.log("Card data loaded. [" + datalength + "]");
} else {
console.log("An error occurred while getting cards: " + ERR);
}
});
kgangadhar
2017-10-29