【发布时间】:2019-07-18 13:34:50
【问题描述】:
我正在尝试在 Mongoose 的模式中实现一个 ObjectId 数组。 我在互联网上搜索,发现这应该可以工作:
import mongoose from 'mongoose';
import Schema from 'mongoose';
const UserSchema = mongoose.Schema({
nickName: {
type: String,
unique: true,
required: true,
},
follows: [{
type: Schema.Types.ObjectId, //HERE
ref: 'User',
default: []
}],
}, {
strict: true,
});
const User = mongoose.model('User', UserSchema);
export default User;
或者这个
follows: {
type: [Schema.Types.ObjectId], // HERE
ref: 'User',
default: []
},
我知道它们并不完全相同,但我没有在这两种情况下工作,而是出现了这个错误:
Invalid schema configuration: `ObjectID` is not a valid type within the array `follows`.
我不知道他为什么告诉我 ObjectID(大写“ID”)无效,因为我没有声明任何这些。
我怎样才能做一个 objectId 数组? 我想要一个 ObjectId 数组,通过引用模式“用户”与用户关注的人
[编辑] 正如 Bhanu Sengar 在评论中提到的那样,我必须在 Schema 之前加上“mongoose”。
[{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
正如 Halil SAFAK 所说,我删除了默认值。
它也不起作用,因为我在两个导入之间存在冲突
import mongoose from 'mongoose';
import Schema from 'mongoose';
【问题讨论】:
标签: arrays node.js mongoose schema objectid