Gaben Lounge
Gaben Lounge

Reputation: 35

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)

Upvotes: 0

Views: 2175

Answers (1)

kgangadhar
kgangadhar

Reputation: 5088

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

Upvotes: 1

Related Questions