简体   繁体   中英

Mongoose schema suddenly invalid

I'm currently working on a MERN Application. I'm fairly new to backend and database related topics.

I have configured this mongoose model, using the following schema:

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;

In my api.js file, this is how I import the model:

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

Now, when starting my server I get following error:

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

I'm wondering why the type is invalid. I am using the syntax from the mongoose docs. I did refactor my code and moved my backend folder. I can't imagine that this is related to the error, but since refactoring, this error appeared. I also tried moving the backend folder back where it was but I still get this error.

You can use another way for type definitions

title: 'String'

or

title: mongoose.Schema.Types.String

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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