我目前正在开发一个MERN应用程序。我对后端和数据库相关的主题相当陌生。
我已经配置了这个猫鼬模型,使用以下模式:
item.model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const itemSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
date: { type: Date, default: Date.now },
meta: {
reports: {type: Number, default: 0}
}
});
const Item = mongoose.model('Item', itemSchema);
module.exports = Item;
在我的api.js
文件中,我是这样导入模型的:
const Item = require('../models/item.model');
TypeError: Invalid schema configuration: `String` is not a valid type at path `title`
我想知道为什么类型无效。我正在使用猫鼬文档中的语法。我确实重构了我的代码并移动了我的后端文件夹。我无法想象这与错误有关,但自从重构以来,这个错误出现了。我也试着把后端文件夹移回原来的位置,但我仍然得到这个错误。
可以用另一种方式定义类型
title: 'String'
或
title: mongoose.Schema.Types.String