Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

mongoose: CastError: Cast to ObjectId failed for value "5875976f8a03b2229c544c50" at path "_id"

$
0
0

我的schema是这样的:

   type: String,
   value: String,
   attributes: [mongoose.Schema.Types.Mixed],
   created_at: {type: Date, default: new Date()},
   updated_at: {type: Date, default: new Date()},
 }); ```
我在库中新建数据,
```{
"_id" : ObjectId("5874b8193501af79a31dbb2b"),
"type" : "user_id",
"value" : "1",
"updated_at" : ISODate("2017-01-10T10:31:30.314Z"),
"created_at" : ISODate("2017-01-10T10:31:30.314Z"),
"attributes" : [
	{
		"source" : "campus",
		"nickname" : "x13015528669",
		"created_at" : ISODate("2017-01-10T10:31:53.971Z")
	}
],
"__v" : 0
},```
然后在代码中:
```let query = {type: 'user_id, value:'1'} ```
拿到上面的那条数据,然后在拿那个_id去查:
```query = {_id: '5874b8193501af79a31dbb2b'}```,
这个时候就会抛出错误:
```CastError: Cast to ObjectId failed for value "5875976f8a03b2229c544c50" at path "_id" for model "vertex"```
这个值:‘5875976f8a03b2229c544c50’已经是ObjectId格式的数据,但是为什么还是会抛出转换错误呢?
stackoverflow上面没有找到合适的答案,有兴趣的朋友麻烦帮忙解决下,谢谢。

Viewing all articles
Browse latest Browse all 14821

Trending Articles