node.js - 面对“throw new TypeError(`Invalid schema configuration:\`${name}\` is not `+”
问题描述
使用 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);
解决方案
这只是对 Mohammed 解决方案的更简洁的解决方案。
type
是定义架构时最重要的对象键,并且您的 todo 字段缺少它。您需要type
像这样设置为 ObjectId
const Schema = mongoose.Schema;
const userSchema = new Schema({
...
todos: [
{
type: Schema.Types.ObjectId,
ref: 'Todo'
}
]
});
推荐阅读
- ignite - 点燃内核状态总是显示停止
- flutter - Flutter sqflite 不适用于真实设备 android
- windows - 环境变量扩展不再适用于 Windows 上的 npm run
- php - PHP8将0与空字符串进行比较,其背后的逻辑是什么
- jenkins - 构建触发器不会保存在 Jenkins 中
- python - 遍历 pandas df 返回所有与正则表达式不匹配的值
- powershell - 如何使用 PowerShell 解析文件夹和文件?
- python - 如何计算已登录的客户端扭曲?
- reactjs - AsyncStorage 不存储数据,随后不检索数据
- javascript - 使用更高分辨率的javascript将画布图表下载为PNG图像