我从Mongoose idGetter函数中收到以下错误。任何人都可以对可能导致它的原因有所了解吗?
return this.$__._id = null == this._id
^
TypeError: Cannot set property '_id' of null
at model.idGetter (e:\Users\Christine\test\mvt\node_modules\mongoose\lib\schema.js:98:23)
错误情况涉及在通过因特网访问的数据库上执行文档删除(在同一集合中的不同文档上)的多个客户端请求(即,响应时间慢)。访问其中一个已删除文档的ID时发生故障。它似乎有一个很小的时间窗口 - 我试图调试它的所有内容都会导致问题“消失”。
申请代码概要:
Details.findOneAndRemove({ _id : remove_id}, afterRemove);
function afterRemove(err, removedDoc){
if (!err && removedDoc){
OtherDetails.find({ _id: some_id}, afterFind);
}
function afterFind(err, foundDoc){
if (!err && foundDoc){
// next line causes exception
if (foundDoc.details_id === removedDoc.id) { // do stuff };
}
}
}
答案 0 :(得分:0)
您是否在已删除的文档上定义了id
属性?
如果没有,您可能希望将foundDoc.details_id
与removedDoc._id
以下是猫鼬代码库中idGetter
函数的源代码:
function idGetter () {
if (this.$__._id) {
return this.$__._id;
}
return this.$__._id = null == this._id
? null
: String(this._id);
}
据我了解,我认为idGetter
专门针对ObjectId
类型,因此例外。
如果文档尚不存在,您还可以尝试在文档上添加id
属性,以查看它是否继续抛出相同的异常。
答案 1 :(得分:0)
fwiw:问题可能与在64位Windows(8.1)上使用32位节点有关。安装64位节点后,我无法重现该问题。
不满意的确定程度,但我想我会分享,以防其他人遇到类似的问题,或者可以说明这是否真的是原因。