0

Here's the LWC JS

   // console.log--> this.tableData = [{"Id":"a4X6t00000006cGEAQ","Sign__c":"No Sign","Notes__c":""}];
            picklistChanged(event) {
                event.stopPropagation();
                let dataRecieved = event.detail.data; 
                const field = dataRecieved.picklistname; // "Sign__c"
                let updateItem =  { Id: dataRecieved.context, [field]: dataRecieved.value };  
        // console.log -->{"Id":"a4X6t00000006cGEAQ","Sign__c":"Existing"}
                this.updateDataValues(updateItem);
            }
        
               updateDataValues(updateItem){
                    let copyData = [... this.tableData];
                    copyData.forEach(item => {
                        if(item.Id === updateItem.Id){
                            item[field] = updateItem[field]; // Here i get the field undefined error
                        }
                    });

So the code above does not work in an LWC but this code below works just fine in codepen.io

        let data = [{"Id":"a4X6t00000006cGEAQ","Sign__c":"No Sign","Notes__c":""}];
                let dataRecieved = { context: "a4X6t00000006cGEAQ", value: "cheeze"};
                const field = "Sign__c";
                let updateItem =  { Id: dataRecieved.context, [field]: dataRecieved.value }; 
            // console.log of updateItem = {"Id":"a4X6t00000006cGEAQ","Sign__c":"cheeze"}
somefunction(updateItem);

                function somefunction(updateItem){
                let copyData = [...data];
                copyData.forEach(item => {
                            if(item.Id === updateItem.Id){
                                item[field] = updateItem[field];
                            }
                        });
    }

the console.log of copyData on codepen comes out as

[{"Id":"a4X6t00000006cGEAQ","Sign__c":"Cheeze","Notes__c":""}];

So it works....

I have no idea how to resolve this, lately if i have buggy JS i'll start doing something in codepen or another web IDE but this is a hard wall.

1 Answer 1

2

You've fallen for a common problem in JavaScript. In your LWC, the "field" property is out of scope, while in the codepen.io example, the variable field is in the "closure" for somefunction. You'll want to pass field to the other method so you can use it:

        picklistChanged(event) {
            event.stopPropagation();
            let dataRecieved = event.detail.data; 
            const field = dataRecieved.picklistname; // "Sign__c"
            let updateItem =  { Id: dataRecieved.context, [field]: dataRecieved.value };  
    // console.log -->{"Id":"a4X6t00000006cGEAQ","Sign__c":"Existing"}
            this.updateDataValues(updateItem, field);
        }
    
           updateDataValues(updateItem, field){
2
  • that did fix it and i now see what i did wrong after looking after googling JS scope. Would you say this is a good JS resource github.com/getify/You-Dont-Know-JS
    – ensir
    Commented Jul 29, 2021 at 15:59
  • @ensir I haven't read it myself, but I have heard of it. It seems like a decent resource to learn from.
    – sfdcfox
    Commented Jul 29, 2021 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .