1

I am storing particular key value in Database. But, While fetching the key value, getting undefined error.

await DbHandler.fetch(codeStatus)
  .then((result) => {
    const codeEnabledObj = result[0];
    console.log('codeEnabledObj', codeEnabledObj);
    let codeEnabled = false;
    if (codeEnabledObj && codeEnabledObj.length > 0) { // this code not executing at all.
      codeEnabled = codeEnabledObj[0].isEnabled;
    }
    console.log('codeEnabled', codeEnabled); // getting false always
    console.log('codeEnabledObj.length[0]', codeEnabledObj.length); // undefined

  })
  .catch((error) => {

  });

The issue is, It's not going inside if condition and throwing error like undefined.

But, If we print the response from db fetch

'codeEnabledObj', { type: 'codeStatus', isEnabled: true } // This is my response

Any suggestions?

5
  • where is this log in code? 'codeEnabledObj.length[0]', undefined Commented Apr 17, 2019 at 10:13
  • firstly, what's the codeEnabledObj.length[0]? I guess that's typo. and are you sure your response is an array? codeEnabledObj Is looks like an object from your answer. Commented Apr 17, 2019 at 10:14
  • Please check updated query. Commented Apr 17, 2019 at 10:16
  • Your response isn't an array? why you checking the length? Commented Apr 17, 2019 at 10:19
  • even dictionary also we can check length, right? can't we? Commented Apr 17, 2019 at 10:22

2 Answers 2

2

Objects don't have length property like an array.

codeEnabledObj.length is wrong

use this,

 Object.keys(codeEnabledObj).length 

EDIT :

codeEnabledObj[0].isEnabled should be only codeEnabledObj.isEnabled

Sign up to request clarification or add additional context in comments.

1 Comment

But, If I tried to get codeEnabledObj values, still I am not able to get.
0

There is no property length in the codeEnabledObj , Moreover its not an Array.. so modifying the condition would work, where isEmpty could be a function used from node package as underscore

if (isEmpty(codeEnabledObj)) { // ... } 

and

codeEnabledObj.isEnabled

Thanks.

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.