[英]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;
对于上面的代码,我的获取路线工作正常。 我能够提取数据库中的所有教授。 但是我的“添加”路线无效。 我无法将记录保存在集合中。 我的状态为400,而邮递员中只有一个空记录,例如“ {}”。 我在这方面做错了什么?
模式存在一些问题:
_id:{
type: String,
defualt: uuidV4()
},
defualt
的拼写错误,并且应引用一个函数; 在您的情况下,您将立即执行该函数,因此该引用是针对单个字符串的,这是不正确的。
这应该更好地工作:
_id : {
type : String,
default : uuidV4
}
模式中还有另一种错字: professroLastName
应该是professorLastName
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.