javascript React Native:尝试分配给只读属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33999948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:06:22  来源:igfitidea点击:

React Native : Attempted to assign to readonly property

javascriptreactjsreact-nativejavascript-objects

提问by bozzmob

I am trying to check if the entry exists in the AsycStore and if it doesn't exist then, I create an object and set it. And there is a return statement that returns the dataobject.

我正在尝试检查 AsycStore 中是否存在该条目,如果不存在,则创建一个对象并设置它。还有一个返回数据对象的返回语句。

Here is the pseudocode-

这是伪代码-

AsyncStorage.getItem("dataobject").then((do) => {
  if(do == undefined)
  {
    console.log("initializing obj + setting data");
    var dobj = "data object";
    dobj.isdirty = true;
    AsyncStorage.setItem("dataobject",dobj);
  }
  return AsyncStorage.getItem("dataobject");
}).done();
},

I am getting an error in console -

我在控制台中收到错误消息 -

W/ReactNativeJS: 'Error: Attempted to assign to readonly property.\n stack: \n  <unknown>

index.android.bun…:53047\n  tryCallOne                           
index.android.bun…:5094\n  <unknown>                            
index.android.bun…:5160\n  <unknown>                            
index.android.bun…:3399\n  callTimer                            
index.android.bun…:2954\n  callImmediates                       
index.android.bun…:3003\n  <unknown>                            
index.android.bun…:2523\n  guard                                
index.android.bun…:2452\n  __callImmediates                     
index.android.bun…:2523\n  <unknown>                            
index.android.bun…:2503\n  guard                                
index.android.bun…:2452\n  invokeCallbackAndReturnFlushedQueue  index.android.bun…:2501\n URL: http://localhost:8081/index.android.bundle?platform=android&dev=true\n line: 53047\n message: Attempted to assign to readonly property.'

I am not really sure what is the readonly property I am trying to assign. The line number that is being warned is this line - "dobj.isdirty = true";

我不太确定我要分配的只读属性是什么。被警告的行号是这一行 - "dobj.isdirty = true";

Should I not add a property to an object? How do I solve this problem? What's the right way of doing it?

我不应该向对象添加属性吗?我该如何解决这个问题?正确的做法是什么?

Screenshot-

截屏-

Attempted to assign to readonly property width=100

尝试分配给只读属性 width=100

回答by Nader Dabit

You can probably accomplish this by using the .catch, which should throw if there is not a value there. Something like:

您可能可以通过使用 .catch 来实现这一点,如果那里没有值,它应该抛出。就像是:

AsyncStorage.getItem("dataobject")
.then((do) => {
  this.setState({
    myData: do.toJSON();
  })
})
.catch(() => {
   console.log("initializing obj + setting data");
   var dobj = {};
   dobj.isdirty = true;
   AsyncStorage.setItem("dataobject",JSON.stringify(dobj));
});

回答by oliversisson

Is dobj a string or an object?

dobj 是字符串还是对象?

var dobj = "data object"; dobj.isdirty = true;

var dobj = "data object"; dobj.isdirty = true;

In the second line, dobj.isdirty is undefined and you're trying to set that ("undefined" is a readonly property of JS).

在第二行中, dobj.isdirty 是未定义的,您正在尝试设置它(“未定义”是 JS 的只读属性)。

Now if you're trying to do

现在如果你想做

var dobj = {}; dobj.isdirty = true;

var dobj = {}; dobj.isdirty = true;

Then instead you should do

那么你应该做

var dobj = {}; dobj['isdirty'] = true;

var dobj = {}; dobj['isdirty'] = true;

OR

或者

var dobj = {isdirty: true};

var dobj = {isdirty: true};