0

I have data returned from apex as a string:

[{"Rank":1,"Points":61,"Name":"Susan Johanson","Id":"0038d00000ANPt7AAH"},{"Rank":2,"Points":23,"Name":"Barry Bronze","Id":"0038d00000ANPtcAAH"}]

I use the below to try and access the different values for each object to create my leaderboard...

export default class Tbg_leaderboard extends LightningElement {
    allTBGContacts;
    error;

    connectedCallback(){
        getRankedContacts()
        .then(result => {
            console.log('>>>>>>>>>>>>' + result);
            this.allTBGContacts = JSON.parse(result);
            console.log('>>>>>>>>>>>>' + this.allTBGContacts);
            console.log('>>>>>>>>>>>>' + this.allTBGContacts[0]);
            console.log('>>>>>>>>>>>>' + this.allTBGContacts[0].id);
           
        })
        .catch(error => {
            this.error = error;
        });
    }

The console.logs seen in the .then() shows as follows:

[object Object],[object Object]
[object Object]
undefined

What do I need to do / change to be able to access these values?

1 Answer 1

2

JavaScript is case sensitive. In your JSON its mentioned as Id whereas you are printing id.

Change it to Id and it should work.

1
  • Wow... what a stupid mistake by me. It's one of 'those' days. Thank you for being a second set of eyes and helping. Appreciate it. Commented Jun 21, 2022 at 10:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.