繁体   English   中英

ZCCADCDEDB567ABAE643E15DCF0974E503Z 架构突然无效

[英]Mongoose schema suddenly invalid

我目前正在开发一个 MERN 应用程序。 我对后端和数据库相关主题相当陌生。

我已经使用以下模式配置了这个 mongoose model:

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文件中,这是我导入 model 的方式:

const Item = require('../models/item.model');

现在,当启动我的服务器时,我收到以下错误:

TypeError: Invalid schema configuration: `String` is not a valid type at path `title`

我想知道为什么类型无效。 我正在使用 mongoose 文档中的语法。 我确实重构了我的代码并移动了我的后端文件夹。 我无法想象这与错误有关,但是自从重构以来,就出现了这个错误。 我还尝试将后端文件夹移回原来的位置,但仍然出现此错误。

您可以使用另一种方式进行类型定义

title: 'String'

或者

title: mongoose.Schema.Types.String

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM