0

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)
3
  • 1
    i think your DATA value is null or undefined thats why its giving that error. Commented Oct 29, 2017 at 4:46
  • Ah can you help me how i can handle that so code just pass it through? Commented Oct 29, 2017 at 5:08
  • You can handle it like this if (!ERR) { if(DATA){ allCards = DATA; console.log("Card data loaded. [" + Object.keys(DATA).length + "]"); }} Commented Oct 29, 2017 at 7:03

1 Answer 1

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.