gpt4 book ai didi

javascript - 创建模式,其字段之一包含引用另一个模式 objectId 的 objectId 和一个字符串

转载 作者:行者123 更新时间:2023-11-28 04:29:50 25 4
gpt4 key购买 nike

这是代码。跟随者和追随者无法工作。换句话说,我无法将像 {"user1", objectId1} 这样的对象推送到这两个数组。是否允许有一个字段同时包含对象 ID 和字符串名称?感谢您的帮助。

The error is: 
{ CastError: Cast to ObjectId failed for value "{ userId: '594ad9106f636d4e244d1f11', username: 'aa' }" at path "followers"
var userSchema = mongoose.Schema({
username: {type: String, unique: true},
password: String,
firstName: String,
lastName: String,
roles: [{
type: String, default: 'USER', enum: ['USER', 'ADMIN']
}],
facebook: {
id: String,
token: String
},
email: String,
phone: String,
websites: [{type: mongoose.Schema.Types.ObjectId, ref: "ProjectWebsiteModel"}],
dateCreated: {type: Date, default: Date.now},
followings: [{username: String, type: mongoose.Schema.Types.ObjectId, ref: "ProjectUsereModel"}],
followers: [{username: String, type: mongoose.Schema.Types.ObjectId, ref: "ProjectUsereModel"}]

}, {collection: "projectuser"});

最佳答案

试试这个:

followings: [{
username: String,
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "ProjectUsereModel"
}
}],
followers: [{
username: String,
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "ProjectUsereModel"
}
}]

...因此您的“following”和“followers”数组将包含具有“username”和“id”属性的对象。

关于javascript - 创建模式,其字段之一包含引用另一个模式 objectId 的 objectId 和一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44714355/

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