6

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}) ;
jfab fab
  • 491
  • 1
  • 8
  • 19

2 Answers2

5

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.

jfab fab
  • 491
  • 1
  • 8
  • 19
1

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.

Paul
  • 35,689
  • 11
  • 93
  • 122