Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查内部调用mysql的for循环是否已完成nodejs_Javascript_Mysql_Node.js_Asynchronous_Promise - Fatal编程技术网

Javascript 检查内部调用mysql的for循环是否已完成nodejs

Javascript 检查内部调用mysql的for循环是否已完成nodejs,javascript,mysql,node.js,asynchronous,promise,Javascript,Mysql,Node.js,Asynchronous,Promise,因此,我有一个来自另一个函数的数组,它传递res,这是一个如下所示的列表: [ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ] 它存储用户id,我想要的是一个查找用户id用户名的函数,并将其存储在另一个数组中。这就是我所拥有的: function getThem(res, params) { var promises = res.map(function (item) { // return array o

因此,我有一个来自另一个函数的数组,它传递
res
,这是一个如下所示的列表:

[ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ]
它存储用户id,我想要的是一个查找用户id用户名的函数,并将其存储在另一个数组中。这就是我所拥有的:

function getThem(res, params) {
        var promises = res.map(function (item) { // return array of promises
            // return the promise:
                    for (i = 0; i < Object.keys(res).length; i++) {
                        console.log("user: ", res[i].UserId);
                        getUsernameFromId(res[users.length].UserId).then(function() {
                            console.log("username: ", res[0].username);
                            users.push(res[0].username); 
                        });
                    }
                }, function (err) {
                    console.error(err);
                });
        Promise.all(promises).then(function () {
            console.log("users: ", users);
            //do something with the finalized list of albums here
        });
    }
函数getThem(res,params){
var promises=res.map(函数(项){//返回承诺数组
//回报承诺:
对于(i=0;i
控制台中的输出:

用户:26
用户:4
用户:26
用户:4
用户:[]
用户名:未定义
用户名:未定义
用户名:未定义
用户名:未定义

那么,我如何等待for循环完成mysql调用呢?也许还有别的办法

编辑:不要介意未定义的用户名,以后很容易修复。请告诉我如何在数组中定义那些未定义的对象(必须假设,因为您的代码似乎使用
res
,就像一个majick对象,在您使用它之前,它拥有您所需的一切)实际的
res
看起来像

[ { UserID: 26 }, { UserID: 4 } ]
getUsernameFromId
返回一个带有
username
属性的对象,如

{ username: 'blah', ...otherproperties }
getThem
可以简单地

function getThem(res, params) {
    return Promise.all(res.map(({UserID}) => getUsernameFromId(UserId).then(({username}) => username)))
    .then(users => {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}
或者在“老派”javascript中

function getThem(res, params) {
    return Promise.all(res.map(function (_ref) {
        var UserID = _ref.UserID;
        return getUsernameFromId(UserId).then(function (_ref2) {
            var username = _ref2.username;
            return username;
        });
    })).then(function (users) {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}
假设(必须假设,因为您的代码似乎使用
res
像一个majick对象,在您使用它之前,它拥有您所需要的一切)实际的
res
看起来像

[ { UserID: 26 }, { UserID: 4 } ]
getUsernameFromId
返回一个带有
username
属性的对象,如

{ username: 'blah', ...otherproperties }
getThem
可以简单地

function getThem(res, params) {
    return Promise.all(res.map(({UserID}) => getUsernameFromId(UserId).then(({username}) => username)))
    .then(users => {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}
或者在“老派”javascript中

function getThem(res, params) {
    return Promise.all(res.map(function (_ref) {
        var UserID = _ref.UserID;
        return getUsernameFromId(UserId).then(function (_ref2) {
            var username = _ref2.username;
            return username;
        });
    })).then(function (users) {
        console.log("users: ", users);
        //do something with the finalized list of albums here
    });
}

查看您的Commo
//返回承诺:
。。。嗯,你不在,我可以把它还给你吗?我从他们那里得到的示例是将其返回到另一个函数中,然后(function(){//for loop here})您的
.map()
回调不会返回承诺。您正在执行
res.map
,在该循环中,您正在对
对象.keys(res)
执行一个循环-看起来各种各样的错误-而且,
[RowDataPacket{UserID:26},RowDataPacker{UserID:4}]
不是有效的javascript任何东西都可以看到你有一个commo
//返回承诺:
…好吧,你不在哪里我可以返回它?我从他们那里得到的示例是将它返回到另一个函数中。然后(function(){//for loop here})你的
.map()
回调不会返回承诺。您正在执行
res.map
并且在该循环中,您正在
对象上执行一个循环。键(res)
-这看起来有各种各样的错误-而且,
[RowDataPacket{UserID:26},RowDataPacker{UserID:4}]
是无效的javascript