0

I need to access the id value in a Javascript object to display a simple message that the user has membership if the id value equals a specific value.

I'm getting Uncaught Type Error not defined 'id' message

In the console it's displayed as

subscriptions: Array(1)
     0: // COMMENT Don't know what this is
       autoRenew: false
       canRenew: false
       expiryDate: "2022-10-26T00:00:00"
       membership:
                 id: "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL"

I'm assuming equivalent JSON is something like:

subscriptions {
    0
           {
       membership: {
           id: "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL"
}
}
}

My Javascript Code

const userObj3 = userObj['subscriptions']['0']['membership']['id'];

if (userObj3 = "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL") {
  greeting = "You have membership";
}


1
  • 1
    Within the if clause the OP wants to use an equality operator either == or ===. Also as with the above code, since the OP within the if clause assigns a value to a constant, the OP should get an TypeError: Assignment to constant variable. Commented Feb 24, 2022 at 0:15

1 Answer 1

1

Your subscriptions are an array, also your comparison inside the if is incorrect and should be == or ===, you could also use dot annotation to traverse the object instead of using brackets on every key.

Using the index as a string instead of a number isn't really wrong, it's just not a good practice.

You might want to think about looping through the subscriptions instead of using the direct index just in case there's multiple subscriptions. But that just depends on how you structure your data and is kinda up to you.

const userObj = {
  subscriptions: [
    {
      autoRenew: false,
      canRenew: false,
      expiryDate: '2022-10-26T00:00:00',
      membership: {
        id: '819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL'
      }
    },
    {
      autoRenew: true,
      canRenew: false,
      expiryDate: '2022-10-26T00:00:00',
      membership: {
        id: '201AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL'
      }
    }
  ]
};

let greeting = "You're not a member";

userObj.subscriptions.forEach((sub) => {
  if (sub.membership.id === '201AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL') {
    greeting = "You're a member";
  }
});

console.log(greeting);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much RenokK, your original code solved the error in console. I still can't get the 'You're a member' sentence to appear on the page using greeting.
Making it appear on the page is completely up to you. Instead of the log just render it wherever you need it, some random container, a snackbar, show a alert, whatever. I'm not really sure how i can help with that :)
@Rich ... please edit your question accordingly, which means ... provide the correct data (not the console log of it) and only the relevant and/or failing code part which operates the data. That's the only way for the audience to properly debug the code and/or provide helpful comments. Everything else is guessing.
I've edited the question so that the Membership id's are consistent, hope this helps.

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.