gpt4 book ai didi

node.js - Mongoose Actor 错误: Cast to ObjectId failed for value at path "_id"

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:20 24 4
gpt4 key购买 nike

这是我的架构:

let mongoose = require('mongoose');
let schema = new mongoose.Schema({
type: String,
value: String,
attributes:[mongoose.Schema.Types.Mixed],
created_at: Date,
updated_at: Date
})

然后我用 {type:'user_id', value: 1} 创建一个新集合,然后我通过查询获取新数据:

{type: 'user_id', value: '1'}

没问题,结果如下:

"_id" : ObjectId("5874c5dbed927207df177d3b"),
"type" : "phone",
"value" : "13811785500",
"updated_at" : ISODate("2017-01-10T11:30:19.266Z"),
"created_at" : ISODate("2017-01-10T11:30:19.266Z"),
"attributes" : [],

然后,我使用集合的 ObjectId 查询保存的集合:

models.findBydId("5874c5dbed927207df177d3b")

它抛出错误:

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

谁能告诉我出了什么问题吗?谢谢。

最佳答案

你应该尝试这个->

models.find({"_id": ObjectId("5874c5dbed927207df177d3b")});

关于node.js - Mongoose Actor 错误: Cast to ObjectId failed for value at path "_id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com