I have imported metadata into an LWC controller :

import SENIORITY_OBJECT from '@salesforce/schema/xx_Seniority__c';


@wire(getObjectInfo, { objectApiName: SENIORITY_OBJECT }) objectInfo;

If I later do:

console.log('scale : '+this.objectInfo.data.fields.xx__Rate__c.scale);

then it fails with a very cryptic error.

But if I 'gently' drill down into the objectInfo, checking each step, then it works:

    if (this.objectInfo) {
        if (this.objectInfo.data) {
            if (this.objectInfo.data.fields) {
                if (this.objectInfo.data.fields.xx__Rate__c) {
                    if (this.objectInfo.data.fields.xx__Rate__c.scale) {
                        console.log('objectInfo.data.fields.xx__Rate__c.scale = '+this.objectInfo.data.fields.xx__Rate__c.scale);

    //the line which fails if the above if() blocks aren't there!!
    console.log('scale : '+this.objectInfo.data.fields.xx__Rate__c.scale);

With the if() blcoks, I can see that the entire structure this.objectInfo.data.fields.xx__Rate__c.scale is present, but the final console.log() can only access it if preceded by the nested if() blocks checking every level.


  • what is this "cryptic" error? can you provide it? Aug 26 at 16:30
  • and how are you trying to get value for scale? inside lifecycle event, or by UI user interaction? Aug 26 at 16:39

You can simplify your code by using the optional chaining operator (?.) which enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

additionally, you can use a nullish coalescing operator (??) at the end of your chain to assign a default value if the chain evaluates to a nullish value.

so, in the end, your object chain would look something like this:

topLevelObject?.nestedObjectProp1?.nestedObjectProp2?.property ?? 'default value'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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