Ryan Weiss
Ryan Weiss

Reputation: 1318

Bluebird promise resolve(data) is undefined in client code

Hiyas. I have a simple app whereby a client is expecting a promise as a result, but upon calling the resolve() method, the promise keeps returning undefined as the result.

The client code:

UsersRepo.findOneAsync({id: id}).then(function(err, result) {
    console.log("UserService promise resolution", err, result);
});

This outputs "null" and "undefined", for err and result, respectively

The code that is doing the work and returning the promise:

findOneAsync: function(args) {
    var where = ""; //omitted

    var promise = new Promise(function(resolve, reject) {
        db.query("Select * from users" + where + " limit 1", function(err, result) {
            var res = { id: 1, username: 'username', firstName: 'First', lastName: 'Last' };

            if(err != null) {
                console.log("REJECT", err);
                reject(err);
            }
            else {
                console.log("RESOLVE", res);
                resolve(null, res);
            }
        });
    });

    return promise;
}

As you can see, I'm just returning some static data for this test (the 'res' variable). In the client code, the console statement always prints out:

UserService promise resolution null undefined

I don't get this. It looks like I'm doing everything correctly: calling the resolve() method with the data, and the client code is using .then(function(err, result)) properly, so it seems. Why isn't data being received from the client end?

Thanks for any help!

==>

Solution:

As mentioned below, Bluebird's reject and resolve only take one argument. The first 'null' was only being seen. Changing the code to 'resolve(res)' worked. Thanks guys.

Upvotes: 3

Views: 1659

Answers (2)

thgaskell
thgaskell

Reputation: 13226

Bluebird's then does not take in an err. Instead, then takes in two functions, the first being for resolve, and the second for being reject. you can also use catch for handling errors.

http://bluebirdjs.com/docs/api/then.html

Edit: @JaromandaX is correct, the resolve function should only take in one parameter.

You should only resolve(res), in addition to not expecting the error object to get passed into the then callback.

Upvotes: 1

Jaromanda X
Jaromanda X

Reputation: 1

Resolve/Reject both accept a single parameter ... hence why your result is undefined, as the second value you pass to resolve is never used, and the first is null

What you want to do instead is

UsersRepo.findOneAsync({id: id}).then(function(result) {
    console.log("UserService promise resolution", result);
}).catch(function(err) {
    console.log("UserService promise error", err);
});

findOneAsync: function(args) {
    var where = ""; //omitted

    var promise = new Promise(function(resolve, reject) {
        db.query("Select * from users" + where + " limit 1", function(err, result) {
            var res = { id: 1, username: 'username', firstName: 'First', lastName: 'Last' };

            if(err != null) {
                console.log("REJECT", err);
                reject(err);
            }
            else {
                console.log("RESOLVE", res);
                resolve(res);
            }
        });
    });

    return promise;
}

Upvotes: 7

Related Questions