admin管理员组文章数量:1429919
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
4 Answers
Reset to default 1I'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
本文标签: javascriptChange Object field value from array in LWCStack Overflow
版权声明:本文标题:javascript - Change Object field value from array in LWC - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745532022a2662104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论