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

标签 javascript android react-native

我正在尝试将一个数组添加到全局数组中。然后将该数组保存到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 - 无法添加属性2,对象不可扩展Array.push,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008665/

相关文章:

Javascript - 获取函数(方法)的类所有者

安卓工作室插件 : Adding icon near SDK manager

react-native - 无法使用 Expo 在移动设备上重新加载 React Native 应用程序

javascript - 将 Shiny unbindAll 和 bindAll 仅限于 HTML 元素

javascript - 解构一个对象并创建带有键名的变量 Javascript

适用于大屏幕的 android 可绘制文件夹

reactjs - 字体大小 React Native

javascript - 如何在 React js 16 中编写纯 javascript?或者我们如何在 React 中使用 document.getElementByClassName?

javascript - Chrome 无法在 ubuntu 上正确编译 java 脚本

android - 需要有关声音处理的帮助