小编haz*_*zer的帖子

ObjectId 的 NodeJS/Mongoose 模式数组

我正在尝试在 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;
Run Code Online (Sandbox Code Playgroud)

或这个

follows: {
          type: [Schema.Types.ObjectId], // HERE
          ref: 'User',
          default: []
  },
Run Code Online (Sandbox Code Playgroud)

我知道它们并不完全相同,但不是在这两种情况下都工作,我有这个错误:

 Invalid schema configuration: `ObjectID` is not a valid type within the array `follows`.
Run Code Online (Sandbox Code Playgroud)

我不知道他为什么告诉我 ObjectID(带有大写“ID”)无效,因为我没有声明任何这些。

我怎样才能做一个 objectId 数组?我想要一个 …

arrays schema mongoose node.js objectid

0
推荐指数
1
解决办法
3844
查看次数

标签 统计

arrays ×1

mongoose ×1

node.js ×1

objectid ×1

schema ×1