简体   繁体   中英

Error with Cannot convert undefined or null to object

The code here i recently cant start the code cause error with cannt convert undefined or null to 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);
    }
});

Error that showed on console

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)

Your Utils.getCardsInSets returning undefined or null DATA . In the above code you are not passing any query params to the method, check whether need to pass any params or not.

If what ever doing is correct, then method is returning undefined/null based on certain condition. Just check for DATA then go for its keys length.

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM