Node.js, RESTIFY, Mongoose, Promise: Trying to $push an array to update but not working Node.js, RESTIFY, Mongoose, Promise: Trying to $push an array to update but not working mongoose mongoose

Node.js, RESTIFY, Mongoose, Promise: Trying to $push an array to update but not working


So, based on :

This code is working without that promise something

i can point a thing or two,

in db.js

 module.exports.pushsession =        async(model, id, data) => {            return new Promise((resolve, reject) => {

you don't need async since you're returning a promise so replace this

async(model, id, data) => {

with

(model, id, data) => {

and since you're returning a promise and removed async , you don't need the await on the other side ( controller.js ), so this

 const result = await usersessionDB.pushsession(model, id, request);            if (result) {                response.success(res, next, result, 200, response.HTTP_STATUS_CODES.ok);            } else {

should be

usersessionDB.pushsession(model, id, request).then(    (result) => { // when resolved        response.success(res, next, result, 200, response.HTTP_STATUS_CODES.ok);    },     (err) => { // when rejected        response.failure(res, next, {                    message: 'ID does not exist'                }, 404, response.HTTP_STATUS_CODES.not_found);    });

this is a comparison between async/await and promises : Javascript Promises vs Async Await. Difference?

and here's some good examples of using promises : https://medium.com/dev-bits/writing-neat-asynchronous-node-js-code-with-promises-32ed3a4fd098

i think your $push is ok but you already said

This code is working without that promise something

i hope this helps and Good luck :)


I tried cleaning my code

here's the controller.js:

/** Push usersession message  */module.exports.pushsession =   async (req, res, next) => {        try {            //jwt.validateToken(req);            var en = "en";            var dateEn = moment().locale(en);            format = "MM/DD/YYYY h:mm:ss A"; //h:mm:ss.SSS if you want miliseconds            var datetime_now = dateEn.format(format);            console.log(datetime_now);            var data = {                     message: req.body.message,                     message_type: req.body.message_type,                    timestamp: datetime_now            };            const model = usersessionDB(req.query['client']);            const id = req.body.fb_id;            console.log(id);            const result = await usersessionDB.pushsession(model, id, data).then(                (result) => { // when resolved                    response.success(res, next, result, 200, response.HTTP_STATUS_CODES.ok);                },                 (err) => { // when rejected                    response.failure(res, next, {                                message: 'ID does not exist'                            }, 404, response.HTTP_STATUS_CODES.not_found);                });        } catch (err) {            response.failure(res, next, err, 500, response.HTTP_STATUS_CODES.internal_server_error);        }    }

Here's the db.js:

const mongoose = require('mongoose');const Schema = mongoose.Schema;const ObjectId = mongoose.Types.ObjectId;const usersessionSchema = new Schema({  fb_id: String,  fb_name: String,  fb_profpic: String,  message_body:[{      message: String,      message_type: String,      timestamp: String  }],  admin: Boolean,  branch: String});/** Push message into message body*/module.exports.pushsession =async(model, id, data) => {    console.log(data);    return new Promise((resolve, reject) => {        model.findOneAndUpdate({fb_id: id}, { $push: { message_body: data }})        .then(res => {            console.log(res);            resolve(res);        })        .catch(err => {            reject(err);            console.log(err);            throw err;        });    });}

Out of the blue after I tried to replace $push with $set then again I replace it with $push, it worked.I don't if there's a difference, or I miss something, feel free to point it out.