Javascript Chrome dev - 无法分配给只读属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26287932/
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-08-22 22:40:32  来源:igfitidea点击:

Chrome dev - Cannot assign to read only property

javascriptangularjsmongodbgoogle-chrome-devtoolstypeerror

提问by jfab fab

I'm building an app using AngularJS, MongoDB and NodeJS. My app uses the Mongolab REST API for CRUD operations. I'm also using Google Chrome Developer Tools for debugging.

我正在使用 AngularJS、MongoDB 和 NodeJS 构建一个应用程序。我的应用程序使用 Mongolab REST API 进行 CRUD 操作。我也在使用 Google Chrome Developer Tools 进行调试。

Until today, my Update operations on mongo were working fine on both Chrome and Firefox (that I use occasionally) but after Chrome updated automatically, the updates fail and I have this error :

直到今天,我在 mongo 上的更新操作在 Chrome 和 Firefox(我偶尔使用)上运行良好,但在 Chrome 自动更新后,更新失败,我有这个错误:

TypeError: Cannot assign to read only property '_id' of {"$inc":{"count":1},"$set":{"messages":[{"unread":false,"flagged":false}]}}
at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.9/angular.js:409:18

I have this error only on Chrome, Firefox shows no error at all and the update is successful. Using strict mode in my angular module the update itself is done using this :

我只在 Chrome 上有这个错误,Firefox 根本没有显示任何错误并且更新成功。在我的 angular 模块中使用严格模式,更新本身是使用这个完成的:

Resource.prototype.$update = function (queryJson,successcb, errorcb) {
  var params = angular.isObject(queryJson) ? JSON.stringify(queryJson) : {},
      httpPromise = $http.put(url + "/" + this.$id(), angular.extend(params,  this, {_id:undefined}), {params:defaultParams});

  return thenFactoryMethod(httpPromise, successcb, errorcb);
};

Where :

在哪里 :

var queryJson = { "$inc": {"count":1} , "$set" : {"messages": message} };

I'm not sure if it's because of the update on Chrome or something else.

我不确定是因为 Chrome 上的更新还是其他原因。

Has anyone come across something like this? Any help will be greatly appreciated.

有没有人遇到过这样的事情?任何帮助将不胜感激。

Note: {_id:undefined} is just a way of removing the _id property from the object. MongoLab requires id of an object to update to be sent as part of the URL and not as part of data sent via PUT.

注意:{_id:undefined} 只是从对象中删除 _id 属性的一种方式。MongoLab 需要将要更新的对象的 id 作为 URL 的一部分发送,而不是作为通过 PUT 发送的数据的一部分发送。

Another way of doing it :

另一种方法:

var objCopy = angular.copy(this) ; 
if (objCopy._id) 
  delete objCopy["_id"] ; 

httpPromise = $http.put(url + "/" + this.$id(), angular.extend(params,  objCopy), {params:defaultParams}) ;

采纳答案by jfab fab

I figured it out. TypeError refered to angular.js line 409 which is about extending an object. what I was doing wrong :

我想到了。TypeError 指的是 angular.js 第 409 行,它是关于扩展对象的。我做错了什么:

angular.extend(params,  objCopy)

So, what I changed ($update method) :

所以,我改变了什么($update 方法):

var params = angular.isObject(queryJson) ? JSON.stringify(queryJson) : {};

for :

为了 :

var params = angular.isObject(queryJson) ? angular.extend({}, queryJson) : {};

httpPromise = $http.put(url + "/" + this.$id(), angular.extend(params,  objCopy), {params:defaultParams}) ;

Instead of copying directly my objCopy to params, I passed an empty object as the target. Object params will be either empty or correctly extended.

我没有直接将 objCopy 复制到 params,而是传递了一个空对象作为目标。对象参数将为空或正确扩展。

回答by Paul

No idea why it's working in one browser and not the other, but you should not be doing this: {_id:undefined}for any reason I can think of.

不知道为什么它在一个浏览器中工作而不是在另一个浏览器中工作,但您不应该这样做:{_id:undefined}出于我能想到的任何原因。