【发布时间】:2021-05-17 15:48:32
【问题描述】:
我目前正在开发一个 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`
我想知道为什么类型无效。我正在使用 mongoose 文档中的语法。 我确实重构了我的代码并移动了我的后端文件夹。我无法想象这与错误有关,但是自从重构以来,就出现了这个错误。我还尝试将后端文件夹移回原来的位置,但仍然出现此错误。
【问题讨论】:
-
看起来你在这段代码之前的某处定义了
String变量。 -
我实际上没有。有没有办法检查这个?这可能与重构有关吗?只是为了尝试,我将
String更改为Number,但仍然遇到同样的错误。 -
我写它是因为我测试了你的代码,它对我来说非常有效。你可以轻松地测试它放在
new Schemaconsole.log(String)之前。 -
这让我更加沮丧:((。登录
String为我返回一个函数,我没有更改任何内容或为String定义任何其他值。还尝试其他类型为@987654333 @ 或Boolean不起作用。我也尝试过使用 mongoose.Schema.Types.String,正如您在其他评论中所建议的那样,但这让我遇到了这个错误:TypeError: Invalid value for schema path `title`, got value "undefined" -
你的猫鼬版本是什么?
标签: javascript node.js mongoose schema