3

This question already has an answer here:

Below is the code tried,

@wire(getObjectDetails)
    wiredResult(data,error) { 
        if(data){
            console.log('DateOfRun-->',data);
            this.lstRecs=data;
            console.log('values len-->',this.lstRecs);
            console.log('values -->',this.lstRecs.values);
        } else if(error){
            console.log('error -->'+error);   
        }
    } 

I am getting data as [object Object],But would like to fetch the values of a record.

i tried below:

 @wire(getObjectDetails)
    wiredResult(data,error) { 
        if(data){
            console.log('DateOfRun-->',data);//[object object]
            this.lstRecs=data;
            console.log('values len-->',JSON.stringify(data));//Getting json value
            this.strtest=JSON.stringify(data);
            console.log('data=='+this.strtest.data);//undefined

        } else if(error){
            console.log('error -->'+error);   
        }
    } 

marked as duplicate by glls, Pranay Jaiswal, Lukas Lunow, Raul, Kevin O'Hara May 14 at 15:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @glls - you are right. I deleted my comment after I realized. – javanoob May 11 at 13:11
7

[object Object], is what you see when you concatenate a JavaScript object with a string. The values you want are probably in that object and you just have to reference them explicitly to see them.

When debugging, this can be a helpful approach to seeing the full data structure:

console.log('Data-->', JSON.stringify(data));

and so know what names to use when you extract values.

  • Thanks Keith,i received data in JSON Format,values len--> {"data":{"Id":"a002E00000ZKKbGQAX","Name":"FCS Group","Frequency__c":"Monthly",}},Now i need to get the value of frequency,could you please tell me how to retreive – Vanaja Kasaragadda May 11 at 12:36
  • 3
    @VanajaKasaragadda E.g. var f = data.Frequency__c;. Note that JavaScript is case-sensitive (unlike Apex). – Keith C May 11 at 12:43
  • 1
    @VanajaKasaragadda - accept the answer if this helps you since it will help other folks as well. – RedDevil May 11 at 17:36
4

@Vanaja - I would strongly recommend you go through the LWC documentation:

and to complete the trailheads on LWC's + ramp up your Javascript skills.

JSON.stringify is NOT a JSON object, So it is normal that your code does nothing...

console.log('DateOfRun-->',data);//[object object]
this.lstRecs=data;
console.log('values len-->',JSON.stringify(data));//NOT JSON Value
this.strtest=JSON.stringify(data);// this.strtest is a STRING
console.log('data=='+this.strtest.data);//undefined because this.strtest is a string and not a JSON Object, no key values here...

Pay carefull attention to @Keit's second comment below his answer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.