Express js promise.all返回undefined

时间:2017-10-15 07:13:31

标签: node.js express promise

嗨,我是新手表示和承诺,我试图使用来自快递路由器的promise.all调用多个异步函数,但它返回undefined,请指导我解决问题。

user.js //路线

var findAllUsersDetails = function(router){
        router.post('/api/v1/users/getAllUserFormDetails', 
        function (req, res) {
            Promise.all([
                userModel.getAllUsers(req), 
                userModel.getAllUsers(req), 
            ])
            .then((data) => console.log(data))
            .catch((err) => console.log(err))
    });
}

user.js // models

var userModel = {
getAllUsers : function(req){
    var string = "";
    var id_company = req['user'].id_company;
    var dbConnection = dbConnectionCreator();
    var getAllUsers = getAllUsersSqlString(string, id_company);
    console.log("ANGEL: finding all employees");

    dbConnection.query(getAllUsers, function(error, results, fields){
        return new Promise((resolve, reject) => {
              console.log(results);
            if (error) {
                dbConnection.destroy();
                console.log("error: ", error);
                return reject (err);
            } else if (results.length === 0) {
                resolve("User not found.");
            } else {
                resolve(results);
                //return (callback({employeeData: results}));
            }
          })
    });     
},
}
module.exports = userModel;

3 个答案:

答案 0 :(得分:1)

Promise应该返回function getAllUsers(req) { return new Promise(function(resolve, reject){ //... }); } a.e。:

dbConnection.query(getAllUsers, function(error, results, fields)

在您的情况下,Promise会返回getAllUsers : function(req){ var string = ""; var id_company = req['user'].id_company; var dbConnection = dbConnectionCreator(); var getAllUsers = getAllUsersSqlString(string, id_company); console.log("ANGEL: finding all employees"); return dbConnection.query(getAllUsers, function(error, results, fields){ // ^^^ return new Promise((resolve, reject) => { console.log(results); if (error) { dbConnection.destroy(); console.log("error: ", error); return reject (err); } else if (results.length === 0) { resolve("User not found."); } else { resolve(results); } }) }); }, ,因此您可以编写如下内容:

 List<Hospitals> result = databaseHelper.getHospitals(this);
 ArrayList<Hospitals> al_HOSPITAL = new ArrayList<>(result.size());
 al_HOSPITAL.addAll(result);

答案 1 :(得分:1)

您的getAllUsers函数应返回Promise,但返回未定义(无)。

承诺将返回dbConnection.query,但不会返回getAllUsers函数。

您可以尝试添加return

return dbConnection.query

如果这不起作用,那么dbquery不会返回返回给它的回调。

您可能需要找到解决此问题的替代方案。

让我知道它是否有效。

答案 2 :(得分:0)

我所做的是将dbconnection置于promise函数中,现在我可以使用dbConnection.query()的结果返回promise

getAllUsers : function(req){
    var string = "";
    var id_company = req['user'].id_company;
    var dbConnection = dbConnectionCreator();
    var getAllUsers = getAllUsersSqlString(string, id_company);
    console.log("ANGEL: finding all employees");

    return new Promise((resolve, reject) => { 
        dbConnection.query(getAllUsers, function(error, results, fields){ //
          console.log(results);
            if (error) {
                dbConnection.destroy();
                console.log("error: ", error);
                return reject (err);
            } else if (results.length === 0) {
                resolve("User not found.");
            } else {
                resolve(results);
                //return (callback({employeeData: results}));
            }
        });
      });

感谢您的即时回复,实际上我理解的问题是因为您的答案会回复承诺。