【发布时间】:2014-12-04 22:41:01
【问题描述】:
我正在使用 AngularJS、MongoDB 和 NodeJS 构建一个应用程序。我的应用程序使用 Mongolab REST API 进行 CRUD 操作。我也在使用 Google Chrome 开发者工具进行调试。
直到今天,我在 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
我只在 Chrome 上出现此错误,Firefox 完全没有显示错误并且更新成功。 在我的角度模块中使用严格模式,更新本身是使用这个完成的:
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);
};
地点:
var queryJson = { "$inc": {"count":1} , "$set" : {"messages": message} };
我不确定是因为 Chrome 的更新还是其他原因。
有没有人遇到过这样的事情?任何帮助将不胜感激。
注意:{_id:undefined} 只是从对象中删除 _id 属性的一种方式。 MongoLab 要求要更新的对象的 id 作为 URL 的一部分发送,而不是作为通过 PUT 发送的数据的一部分发送。
另一种方法:
var objCopy = angular.copy(this) ;
if (objCopy._id)
delete objCopy["_id"] ;
httpPromise = $http.put(url + "/" + this.$id(), angular.extend(params, objCopy), {params:defaultParams}) ;
【问题讨论】:
标签: javascript angularjs mongodb google-chrome-devtools typeerror