2

I have following schema in mongoose

var mongoose = require("mongoose");

var Schema = mongoose.Schema;
var UserSchema = new mongoose.Schema({
  email: { type: String, unique: true },

});
var User = mongoose.model('User', UserSchema);
var ReviewSchema = new Schema({
  schoolId: String,
  firstname: String,
  image_url: String,
  lastname: { type: String },
  email: String,
  rating: Number,
  review: String,
  time: { type: Date, default: Date.now },
  updated_at: Date,
  voters: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]

});


var Review = mongoose.model('Review', ReviewSchema);


module.exports = Review;

and following code in nodejs

app.post("/like", function (req, res) {
    var query = { id: mongoose.Types.ObjectId(req.body.id) };
    var voter = { email: req.body.email };
    var update = { $push: { 'voters': voter } };
    var options = { upsert: true, 'new': true };

    Review.findOneAndUpdate(query, update, options, function (err, doc) {
        console.log(err);
        res.end("done");
    });
});

When I am sending request to above method I am getting following error message

 { [CastError: Cast to ObjectId failed for value "[object Object]" at path "voters"]
     message: 'Cast to ObjectId failed for value "[object Object]" at path "voters"',

1 Answer 1

5

You have voters defined as an ObjectId ref, but you're trying to store an actual User representation inside it.

Assuming I understand your links correctly, you'll want to lookup the _id of the User by req.body.email, then make your update { $push: { voters: userId } } (where userId is the id of the User you found).

7
  • I am finding review by id and then I am pushing email address to voters array Commented Aug 31, 2016 at 17:19
  • If you want to store the email address on the voters array, you'll have to modify your definition to store the object itself, instead of a reference to the User model. The way you have it right now, your model should be storing User._id inside Review.voters[].
    – dvlsg
    Commented Aug 31, 2016 at 17:20
  • If I am not wrong voters should be defined as with voters : [UserSchema ] Commented Aug 31, 2016 at 17:22
  • I think you can do that. Truth be told, I've never actually attempted to re-use a schema as an internal schema as well as a directly exported model at the same time, but I imagine it should work. If you run into problems with pointers or something, you could also define a ReviewVoterSchema that also holds an { email } field.
    – dvlsg
    Commented Aug 31, 2016 at 17:24
  • 1
    Its creating a new document because its not able to find the document using the query var query = { id: mongoose.Types.ObjectId(req.body.id) };, since mongoDb stored ids in the field _id. Update your query with var query = { _id: mongoose.Types.ObjectId(req.body.id) }
    – Ananth Pai
    Commented Aug 31, 2016 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.