javascript - Mongoose 架构突然失效
问题描述
我目前正在开发一个 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 文档中的语法。我确实重构了我的代码并移动了我的后端文件夹。我无法想象这与错误有关,但是自从重构以来,就出现了这个错误。我还尝试将后端文件夹移回原来的位置,但仍然出现此错误。
解决方案
您可以使用另一种方式进行类型定义
title: 'String'
或者
title: mongoose.Schema.Types.String
推荐阅读
- sql - 查找行之间的时间差
- python - AWS 上的训练数据
- cocoa - 如何在没有动画的情况下滚动 NSScrollView?
- c# - C DLL。如何在 C# 中导入
- javascript - 每半秒反应一次原生 SetState 数组 使应用程序挂起大数据
- mysql - MySQL JSON - 如果匹配条件则替换值
- python - 如何从另一个驱动器或切换驱动器在 CMD 中运行 python 脚本/程序?
- node.js - 为什么mongodb返回成功并且它没有将值发布到mongodb数组中
- php - PHP OOP 使用 Mysqli 和 Class 打印单个值
- rest - 设计为聚合多个 REST API 并将数据发送到 UI