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.
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 :
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 :
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.
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.
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}) ;
The error "Cannot assign to read only property of object" occurs when we try to change a property of an object that has been frozen or when a property has been defined with Object. defineProperties() . To solve the error, create a copy of the object or array, or set the property to writable .
This JavaScript exception is read-only works in strict mode-only and It occurs if a global variable or object property which has assigned to a value, is a read-only property. Cause of Error: The global variable or object property that has assigned value is a read-only property.
A read-only property means it cannot be overwritten or assigned to. Any such assignment will silently do nothing in non-strict mode. E.g.: var obj = {}; Object.
I figured it out. TypeError refered to angular.js line 409 which is about extending an 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 my objCopy to params, I passed an empty object as the target. Object params will be either empty or correctly extended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With