javascript - Change Object field value from array in LWC - Stack Overflow

I can't update object field value in array in this part.this.wiredProducts[0].Price__c = this.sel

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;
    
      }
    }  

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;
    
      }
    }  
Share Improve this question edited Jan 5, 2021 at 19:58 B.S. 6781 gold badge7 silver badges15 bronze badges asked Jan 5, 2021 at 19:53 python_newbiepython_newbie 231 silver badge7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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

As noted in the documentation:

Objects passed to a ponent are read-only. To mutate the data, a ponent 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.

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

this.wiredProducts = {...data.productList};

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

See this: Reactivity for Fields, Objects, and Arrays

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745532023a4631738.html

相关推荐

  • javascript - Change Object field value from array in LWC - Stack Overflow

    I can't update object field value in array in this part.this.wiredProducts[0].Price__c = this.sel

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信