Chrome dev - unable to set read-only property

I am building an application using AngularJS, MongoDB and NodeJS. My application uses the Mongolab REST API for CRUD operations. I also use the Google Chrome developer tools for debugging.

Until today, my update operations on mongo worked fine in both Chrome and Firefox (which I sometimes use), but after Chrome is updated automatically, the updates fail, and I have this error:

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 in Chrome, Firefox does not show any error and the update was successful. Using strict mode in my angular module, the update itself is done using this:

 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 this is due to an update in Chrome or something else.

Has anyone come across something similar? Any help would be greatly appreciated.

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

Another way to do this:

 var objCopy = angular.copy(this) ; if (objCopy._id) delete objCopy["_id"] ; httpPromise = $http.put(url + "/" + this.$id(), angular.extend(params, objCopy), {params:defaultParams}) ; 
+6
source share
2 answers

I get it. TypeError refers to the angular.js 409 line, which is to extend the object. what i was doing wrong:

 angular.extend(params, objCopy) 

So what I changed ($ update method):

 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 objCopy to params, I passed an empty object as a target. The object parameter will be either empty or valid.

+5
source

I donโ€™t know why it works in one browser and not in another, but you should not do this: {_id:undefined} for any reason I can think of.

+1
source

Source: https://habr.com/ru/post/976527/


All Articles