1

I can't update object field value in array in this part.

this.wiredProducts[0].Price__c = this.selectedRate;

I can get this value but can't do anything with it. It throws such error:

[NoErrorObjectAvailable] Script error.

So may be someone knows something about this issue. The code is below.

    import { LightningElement, wire, track, api } from 'lwc';  
    import getActiveProducts from '@salesforce/apex/GetActiveProducts.getSearchedProducts';  
    export default class IterationInLwc extends LightningElement {  
      @api searchedName;
      @api searchedPrice;
      wiredProducts;
      rates;
      @api selectedRate = 1;
      @track searchedProducts;
      @track error;
      @wire(getActiveProducts)
      wiredProduct({ error, data }) {
        if (data) {
            console.log(data);
            this.wiredProducts = data.productList;
            this.rates = data.rates;
            
            this.searchedProducts = this.wiredProducts;
            console.log(this.prices);
            
            console.log(this.searchedProducts);
        } else if (error) {
            this.error = error;
        }
     }
    
      handleSearchByName(event){
        this.searchedName = event.target.value;
        this.searchedProducts = this.wiredProducts.filter
        (product => product.Name.includes(this.searchedName) && product.Price__c >= this.searchedPrice);
      }
    
      handleSearchByPrice(event){
        this.searchedPrice = parseFloat(event.target.value);
        this.searchedProducts = this.wiredProducts.filter
        (product => product.Name.includes(this.searchedName) && product.Price__c >= this.searchedPrice);   
      }
      handleMenuSelect(event) {
        this.selectedRate = event.detail.value;
        this.wiredProducts[0].Price__c = this.selectedRate;
    
      }
    }  
B.S.
  • 668
  • 1
  • 5
  • 15
python_newbie
  • 23
  • 1
  • 7

4 Answers4

1

I've found the solution. The problem was here `

this.wiredProducts = data.productList;

I changed this into

this.wiredProducts = JSON.parse(JSON.stringify(data.productList)); 

And everything became fine. I guess that data wich we recieve from wire are read only. So we can unlock it by this trick. If someone knows other solutions please post it here

python_newbie
  • 23
  • 1
  • 7
1

As noted in the documentation:

Objects passed to a component are read-only. To mutate the data, a component should make a shallow copy of the objects it wants to mutate

So the solution you came with is the proper idea, but not the correct implementation. To create a shallow clone of the record, you should use the spread operator or Object.assign(). Check this for an example.

1

you can try this way also and by this no need to parse and stringify the JSON

this.wiredProducts = {...data.productList};
Amrit Jain
  • 11
  • 1
0

I had a similar issue and it was related to Reactivity for Fields, Objects, and Arrays:

See this: Reactivity for Fields, Objects, and Arrays

AmirAdel
  • 35
  • 6