简体   繁体   English

无法添加属性 2,对象不可扩展 Array.push

[英]Can not add property 2, object is not extensible Array.push

I'm trying to add an array to a global array.我正在尝试将数组添加到全局数组。 Then save that array to AsyncStorage .然后将该数组保存到AsyncStorage But I can't seem to be able to do that and I'm not sure why.但我似乎无法做到这一点,我不知道为什么。

It seems to have a problem with pushing to that array.推送到该数组似乎有问题。

I have tried pushing any key to it, but still that didn't fix my problem.我试过按任何键,但这仍然没有解决我的问题。

//for saving all transactions

let exchanges =[ x = ''];
class AddScreen extends React.Component {

constructor(props) {
i=0;
super(props);

this.state = {text: '',  name:'',amount:'',budget:'',date:'',geoloc:''};
}

setName = () => {
const {text} = this.state;
AsyncStorage.setItem('name', text);
alert("data Saved " +text);
}

SavetoGlobalTransaction = () => {
//get everything from state
const {name} = this.state;
const {amount} = this.state;
const {budget}= this.state;
const {date}= this.state;
const {geoloc}= this.state;

trans = {
  name :this.name,amount:this.amount,budget:this.budget,date:this.date,geoloc:this.geoloc
}

exchanges.push(trans);

AsyncStorage.setItem('ex', exchanges);
alert("data Saved " +exchanges[0].name);
}

This is the kind of error that I get:这是我得到的那种错误:

Cannot add property 2, object is not extensible
    Array.push
     <anonymous>
    Object.AddScreen._this.SavetoGlobalTransaction [as onPress]
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:2177:19
    Object.touchableHandlePress
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:15410:40
    Object._performSideEffectsForTransition
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:14990:16
    Object._receiveSignal
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:14916:14
    Object.touchableHandleResponderRelease
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:14795:12
    Object.invokeGuardedCallbackImpl
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:27408:16
    invokeGuardedCallback
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:27499:37
    invokeGuardedCallbackAndCatchFirstError
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:27503:31
    executeDispatch
     f6f1eeda-c1a7-4b01-ba0e-76dc313c6ebd:27697:9

You cannot modify the array, you need to create a new array with the additional data and save that.您不能修改数组,您需要使用附加数据创建一个新数组并保存。 Try using .concat or the spread operator.尝试使用 .concat 或扩展运算符。

I would recommend to just use spread operator(...) and it will just bomb the values of your trans object and add it into the exchange array我建议只使用扩展运算符(...),它只会轰炸您的 trans 对象的值并将其添加到交换数组中

exchanges.push(...trans);

This should do the work.这应该可以完成工作。

I have hit the same error as yours.我遇到了和你一样的错误。 My way to resolve this is to deep clone the original array to a new one and then operate the new array and use the new array as the data source.我解决这个问题的方法是将原始数组深度克隆为一个新数组,然后操作新数组并使用新数组作为数据源。

As for your code, you can deep clone the AsyncStorage and use the new array to insert your previous array.至于你的代码,你可以深度克隆AsyncStorage并使用新数组插入你以前的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法添加属性 0,object 在 Array.push 不可扩展 - Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push 无法添加属性_tracking,对象不可扩展 - Can't add property _tracking, object is not extensible 无法添加属性 0,尝试将类构造的对象推入数组列表时对象不可扩展 - React/NextJs - Cannot add property 0, object is not extensible when trying to push class-constructed objects indo an array list - React/NextJs array.push 不工作 - 无法添加到数组 - array.push not working - unable to add to array Array.push(Object)插入对象数组 - Array.push(Object) inserts array of objects 未捕获的TypeError:无法添加属性12,对象不可扩展 - Uncaught TypeError: Can't add property 12, object is not extensible Javascript array.push()不添加而是替换它 - Javascript array.push() do not add but replace it Array.push 没有正确添加项目 - Array.push does not add item to it correctly 为什么用 array.push() 来做一个 object? - Why array.push() is used to make an object? React Error(TypeError):无法添加属性上下文,对象不可扩展 - React Error (TypeError): Can't add property context, object is not extensible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM