0

I have a document being passed into a function which has the format 0:{username:"test", name:"TestName"} and everything prints in the console properly, but when I try to access userName from it I get an undefined error.

My code currently:

const newuserDocs = result.docs;         

console.log(newuserDocs);

if(this.newUserDocs.username === "test"){
  console.log('THE USERNAME IS GOOD');
} 

I know that newUserDocs is set because I can dump it in the console or elsewhere just fine but trying to do this comparison is failing at the .username portion

What am I doing wrong

2 Answers 2

1

Seems that keyword this is ambigous, as soon as newuserDocs is defined as variable you can access is without this. portion as soon as context for this is unclear in your example:

if(this.newUserDocs.username === "test"){
  console.log('THE USERNAME IS GOOD');
} 

UPD: js is case sensitive. Your variable is called newuserDocs (lower-case U) and then your access it by newUserDocs (upper-case U). Try newuserDocs.username

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

6 Comments

If i try just as newUserDocs.username then it says newUserDocs is undefined
js is case sensitive. Your variable is called newuserDocs (lower-case U) and then your access it by newUserDocs (upper-case U). Try newuserDocs.username
oh wow, not sure how I missed that. however, I changed it and it still says 'cannot read property 'username' of undefined
Wait, I got it to clear the error due to the same issue with userName. But it's not printing my console even though the username matches
Hmm, And the last idea accoding to your data format, newuserDocs[0].username
|
1

I run this, it works perfect. You missed:

  1. You have to approach the variable without use this..
  2. The spelling of the const.

    const newuserDocs = {username:"test", name:"TestName"};
    
    console.log(newuserDocs);
    
    if(newuserDocs.username === "test"){
      console.log('THE USERNAME IS GOOD');
    } 
    

enter image description here

I hope it helps you.

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.