无法将文档保存到集合中 - 猫鼬

Unable to save a document into a collection - mongoose

我有一个教授猫鼬模型,看起来像这样。

var mongoose = require('mongoose');
var uuidV4 = require('uuid/v4');
var Professors = mongoose.model('Professors', {
    _id:{
        type: String,
        defualt: uuidV4()
    },
    professorfirstName: {
        type: String,
    },
    professroLastName: {
        type: String,
    },
    reviews: [
        {
            review: {
                type: String
            },
            reviewer: {
                type: String
            },
            rating: {
                type: Number
            }
        }
    ],
    ratings: {
        type: Number
    }
}); 

module.exports = {
    Professors: Professors
}

我的教授文件路径如下:

var {Professors} = require('./../server/models/professors');
var {mongoose} = require("./../server/db/mongoose");
var router = require('express').Router();

router.get('/getAllProfessors', (req, res) => {
    Professors.find().then((data) => {
        res.send({data});
    }, (err) => {
        res.status(400).send(err);
    });
});

router.post('/add', (req, res) => {
    var professor = new Professors({
        professorfirstName: "test",
        professorlastName: "test",
        reviews: [],
        ratings: 5
    });

    professor.save().then((data) => {
        res.send({data});
    }, (err) => {
        console.log("Didn't save!!");
        res.status(400).send(err);
    });
});

module.exports = router;

对于上面的代码,我的 get 路由工作正常。我能够提取数据库中的所有教授。但是我的 'add' 路线不起作用。我无法将记录保存在集合中。我的状态为 400,而且我在邮递员中只收到一条空记录,如“{}”。我做错了什么?

您的架构存在一些问题:

_id:{
    type: String,
    defualt: uuidV4()
},

defualt 拼写错误,而且,它应该引用一个函数;在您的情况下,您正在立即执行该函数,因此引用是对单个字符串的引用,这是不正确的。

这应该会更好:

_id : {
    type    : String,
    default : uuidV4
}

架构中还有另一个拼写错误:professroLastName 应该是 professorLastName