最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论