Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Return promise after mongoose model saving

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);
            }
        });
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!