【发布时间】:2022-01-14 13:25:42
【问题描述】:
这是我的 userPost.js 架构:
const mongoose = require("mongoose");
let userPostSchema = new mongoose.Schema({
id:{
type: mongoose.Types.ObjectId
},
body:{
type: String
}
}, {timestamps: true});
const UserPost = mongoose.model("post", userPostSchema);
module.exports = UserPost;
这是我的 userAccount.js 架构:
const userPost = require("./UserPost");
const mongoose = require("mongoose");
let userAccountSchema = new mongoose.Schema({
id:{
type: mongoose.Types.ObjectId
},
name:{
type: String
},
posts:
{
type: [userPost]
}
}, {timestamps: true});
let userAccount = mongoose.model("account", userAccountSchema);
module.exports = userAccount;
我收到帖子错误:
node_modules\mongoose\lib\schema.js:984
throw new TypeError('Invalid schema configuration: ' +
^
TypeError: Invalid schema configuration: `model` is not a valid type within the array `posts`.See http://.../mongoose-schematypes for a list of valid schema types.
at Schema.interpretAsType (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:984:15)
at Schema.path (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:677:27)
at Schema.add (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:495:12)
我在 userAccount.js 中使用了 2 个模式 userPost.js。 有什么问题?
为什么会出现以下错误:
TypeError:无效的架构配置:model 不是数组 posts 中的有效类型
我尝试查阅以下链接,尤其是 Mongoose 官方文档的第 3 段代码摘录: https://mongoosejs.com/docs/schematypes.html#arrays
我更改了以下代码:
posts:[
{
type: userPost
}
]
收件人:
posts:
{
type: [userPost]
}
但仍然出现同样的错误。
【问题讨论】:
标签: javascript node.js mongodb express mongoose