在客户端代码中未定义Bluebird Promise(数据) [英] Bluebird promise resolve(data) is undefined in client code

查看:70
本文介绍了在客户端代码中未定义Bluebird Promise(数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiyas.我有一个简单的应用程序,客户期望结果是一个承诺,但是调用了resolve()方法后,承诺会一直返回未定义的结果.

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.

客户端代码:

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

这将分别为err和结果输出"null"和"undefined"

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;
}

如您所见,我只是为此测试返回一些静态数据("res"变量).在客户端代码中,控制台语句始终打印出来:

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

我不明白这一点.看来我做的一切正确:调用数据的resolve()方法,并且客户端代码正确使用了.then(function(err,result)),看来.为什么没有从客户端接收数据?

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?

感谢您的帮助!

==>

解决方案:

如下所述,Bluebird的拒绝和解决只采用一个论点.只看到第一个空".将代码更改为"resolve(res)"有效.谢谢大家.

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.

推荐答案

解析/拒绝都接受一个参数...因此,为什么您的结果未定义,因为从未使用传递给解析的第二个值,而第一个为空

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

您想做的是

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;
}

这篇关于在客户端代码中未定义Bluebird Promise(数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆