[英]Error with Cannot convert undefined or null to object
我最近在這里無法啟動代碼,導致無法將undefined或null轉換為object導致錯誤:
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)
您的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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.