【发布时间】:2021-11-04 20:02:57
【问题描述】:
使用 Typescript 进入 NodeJS。所以主要问题是我正在尝试使用 Mongoose 遵循一对多文档结构。但正如问题所说,我面临这个问题:
throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
TypeError: Invalid schema configuration: `Todo` is not a valid type at path `ref`
这是型号代码:
const Schema = mongoose.Schema;
const userSchema = new Schema({
_id: Schema.Types.ObjectId,
firstname: {
type: String
},
lastName: {
type: String,
},
email: {
type: String,
required: "Enter Email ID"
},
password: {
type: String,
required: "Enter Password"
},
todos: [
{
ref: 'Todo',
_id: Schema.Types.ObjectId
}
]
});
const todoSchema = new Schema({
_id: Schema.Types.ObjectId,
title: {
type: String,
required: "Enter a title"
},
createdAt: {
type: Date,
default: Date.now
},
content: {
type: String
}
})
export const Todo = mongoose.model('Todo', todoSchema);
export const User = mongoose.model('User', userSchema);
【问题讨论】: