3

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);
4
  • Can you also show how you defined the schema for the Review model? Commented Dec 19, 2016 at 10:32
  • @chridam I have updated my post with model Commented Dec 19, 2016 at 10:41
  • 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. Commented Dec 19, 2016 at 10:47
  • @chridam got it thanks. please add it in answer Commented Dec 19, 2016 at 11:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.