I am trying to push data to readBook array in my User collections document. As seen in code bellow and console.log return both User is retrieved from db and there is data to push, however nothing is changed after code finishing to work. No error in console is shown. Please help!
I got a mongodb Schema:
const UserSchema = new mongoose.Schema({ _id: Number, type: { type: String, required: true, default: "personal" }, email: { type: String, unique: true, required: true, trim: true }, username: { type: String, unique: true, required: true, trim: true }, name: { type: String, required: true, trim: true }, password: { type: String, required: true, }, readBook: [{ id: { type: String, }, status: { type: String }, rating : { type: String } }] }); And trying to push data to it using express:
router.post("/ratebook", (req,res,next) => { User.findById(1) .exec( (error, user) => { if(error) { return next(error); } else { console.log(user); console.log(req.body); User.update( { _id: 1}, { $push: {"readBook": req.body}}); } }) }) console.log return:
{ type: 'admin', readBook: [], _id: 1, email: ‘[email protected]', username: 'username', name: ’Name Surname', password: '$2a$10$JcuZNrjEh4KNOZ04TxIKLOy4/hcCK0It0.lAAE4zGN/qPavBFx8GS', __v: 0 } { id: '1', bookRating: '4’ }