Hi I am trying to add a document in collection but it is not adding, I have validated JSON in Robomongo it validates. I have done the same thing with other model it works fine but fore some reason it is not working here can anyone see what is the error.
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();
}
if I remove Product and ads, the function works fine.
Model:
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);
In your schema definition, change this line
ads: {
type: String
},
to
ads: {
type: { type: String }
},
as the ad has a property called 'type', which is reserved for a mongoose type.
Also, the model is expecting a valid ObjectId string representation for the product_id, and not an empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With