1

I'm trying to write a function to create a new user on my database (mongodb) and then return that user as an object which I defined. However, since the call to insert the user is async, result.nInserted is always undefined because the async code doesn't finish in time. Therefore, I never get to return my user. I would try and use callbacks to fetch the user after it is done being inserted, but then how would I be able to return that data from my original addUser function?

User.addUser = function(db, email, username, password, firstname, lastname, company, twitter){
    var _id = new mongodb.ObjectID();

    var result = db.get('users').insert({
        _id: _id,
        email : email,
        verified : false,
        password : password,
        username : username,
        firstname : firstname,
        lastname : lastname,
        company : company,
        twitter : twitter,
        rank : config.RANK_USER
    });

    return result.nInserted == 1 ? new User(_id, email, false, username, firstname, lastname, company, twitter, config.RANK_USER) : false;
};

2 Answers 2

2

From documentation:

Insert

Records can be inserted to a collection with insert

collection.insert(docs[[, options], callback])

So what you want can be done via callback function.

I think you can solve your task

User.addUser = function(db, email, username, password, firstname, lastname, company, twitter, callback){
var _id = new mongodb.ObjectID();

db.get('users').insert({
    _id: _id,
    email : email,
    verified : false,
    password : password,
    username : username,
    firstname : firstname,
    lastname : lastname,
    company : company,
    twitter : twitter,
    rank : config.RANK_USER
}, callback);
};

So callback function will be called when insertion is completed.

It receive two arguments: error and result.

function callback(err, res){
  // some actions
}

Read this question, it can help!

Sign up to request clarification or add additional context in comments.

2 Comments

So is there no way to make it as simple as User.addUser traditionally returning a new user object? I wanted to do this because it is neater and easier to manage, but if it's not doable then I'll try callbacks instead
No, you cannot return a result of an asynchronous operation (you can return whatever you want, but the result will obviously not be available, so your options are limited). The closest to this is using a Promise, but it's not the same. Alternatively, once ECMA Sscript 6 is fully supported, you will be able to do this using a generator.
0

You have to use a promise pattern here:

return result.then(function( myNewUser ){
   return new User(myNewUser._id, email, false, username, firstname, lastname, company, twitter, config.RANK_USER);
})

3 Comments

Doing this seems to return nothing still. I even put a console.log inside of the promise callback and nothing was logged.
Scratch that, I did the most dumb thing possible and put the log after the return. It seems to be returning the right stuff, but my user object located at var user = User.addUser(...); is still undefined. Does this need a promise too, like var user = User.addUser(...).then(function(user){return user;});?
Yes, you're essentially dealing with promises and async pattern here so you'll have to use callbacks. Depending on what your use-case is, you can for example resolve user requests from the callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.