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"',