您好我正在尝试在集合中添加文档,但它没有添加,我在Robomongo验证了JSON验证。我已经用其他模型做了同样的事情它工作得很好但是由于某些原因它不能在这里工作,任何人都可以看到错误。
exports.add_ads = function(req, res) {
if (Object.keys(req.body).length == 0 ||
req.body.user_id == undefined || req.body.user_id == "" ||
req.body.rate == undefined || req.body.rate == "" ||
req.body.ads.type == undefined || req.body.ads.type == "") {
res.status(404).send({ error: "One or more request peremeter is empty" });
}
// console.log(req.body.ads['type']);
new Review({
user_id: "58492c6f05c095160e37436c",
ads: {
type: "view"
},
product: {
product_id: "",
review: "",
rating: 0
},
rate: 0.07,
isActive: true
}).save();
res.end();
}
如果我删除了产品和广告,则该功能正常。
型号:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = require('../models/user.js');
var Product = require('../models/product.js');
// define model =========================================================================
var ReviewSchema = new Schema({
user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
ads: {
type: String
},
product: {
product_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'},
review: String,
rating: Number
},
rate: Number,
isActive: Boolean,
dateCreated: Date
});
module.exports = mongoose.model('Review', ReviewSchema);
答案 0 :(得分:1)
在架构定义中,更改此行
ads: {
type: String
},
到
ads: {
type: { type: String }
},
因为ad
有一个名为' type'的属性,它是为猫鼬类型保留的。
此外,该模型期望ObjectId
的有效product_id
字符串表示,而不是空字符串。