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}) ;