Home > Blockchain >  Return promise after mongoose model saving
Return promise after mongoose model saving

Time:10-17

I am trying to return a promise after model saving in mongoose.

Here is the schema of the User Document :

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const UserSchema = new mongoose.Schema({
    first_name:String,
    last_name:String,
    phone_number:String,
    email:{type:String, unique:true, required:true, lowercase:true},
    account_status: Boolean,
    hash:String,
    salt:String,
});

module.exports = UserSchema;

Here is the code of the User Model:


const mongoose = require("mongoose");
const userSchema = require("./UserSchema");
const userModel = new mongoose.model("User", userSchema);
module.exports = userModel;

Here is the code to save a User Object :

const UserModel = require("../models/user/UserModel");

function UserController(){}

UserController.prototype.addUser = function(user_datas){
    var new_user = new UserModel();
    new_user.first_name = user_datas.first_name;
    new_user.last_name = user_datas.last_name;
    new_user.phone_number = user_datas.phone_number;
    new_user.email = user_datas.email;
    new_user.account_status = false;
    new_user.hash = user_datas.hash;
    new_user.save(function(err, user){
        if(err){
            /** 
               Some customs code before returning the promise 
            **/

            return new Promise(function(resolve, reject){
                reject(err);
                resolve(null);
            });
        }
        
        if(user){

            /** 
               Some customs code before returning the promise 
            **/

            return new Promise(function(resolve, reject){
                resolve(user);
                reject(null);
            });
        }
    });
}

The function addUser always return undefined instead of a promise. What i want is a promise that resolve the User Object created and reject an error if it occured.

Any idea ?

Thanks in advance.

CodePudding user response:

What's happening in your code is that when you return the Promise, that is actually the return value of the callback function passed to new_user.save().

If you want your addUser function to return a Promise that resolves to a User object, you wrap the new_user.save() inside of a Promise and return that instead.

UserController.prototype.addUser = function(user_datas){
    var new_user = new UserModel();
    new_user.first_name = user_datas.first_name;
    new_user.last_name = user_datas.last_name;
    new_user.phone_number = user_datas.phone_number;
    new_user.email = user_datas.email;
    new_user.account_status = false;
    new_user.hash = user_datas.hash;

    return new Promise(function(resolve, reject) {
        new_user.save(function(err, user) {
            if (err) {
                reject(err);
            } else {
                resolve(user);
            }
        });
    });
}

CodePudding user response:

Wrap whole addUser function in return new Promise() then inside save callback resolve/reject

  • Related