javascript - 无法添加属性 2,对象不可扩展 Array.push
问题描述
我正在尝试将数组添加到全局数组。然后将该数组保存到AsyncStorage
. 但我似乎无法做到这一点,我不知道为什么。
推送到该数组似乎有问题。
我试过按任何键,但这仍然不能解决我的问题。
//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);
}
这是我得到的那种错误:
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
解决方案
您无法修改数组,您需要使用附加数据创建一个新数组并保存。尝试使用 .concat 或扩展运算符。
推荐阅读
- javascript - 需要帮助在用于排序的 ul 列表中按索引调用表格行和单元格
- python - 如何计算python中两点数组之间的重叠长度?
- javascript - 如何从 select2 下拉列表中删除用户输入
- azure - 如何从 azure 管道中选择特定的构建代理来运行您的构建?
- java - 尝试刷新 Google Access 令牌时收到 401 Unauthorized
- javascript - 条在网格线之间居中,标签位于网格线下方 (chart.js)
- javascript - 承诺链调用和单独调用之间的区别?
- arrays - 用正确的返回写矩阵递归?
- html - 样式占位符文本 - 输入日期标签
- jquery - jQuery on() 不适用于禁用字段集中的元素