承诺mysql返回承诺

uqzxnwby  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(410)

我即将引爆这个问题的导火索:我打算将一个php应用程序翻译成angular6,但是我会在javasript es6中开发这个问题,以便在最终确定的代码之前进行简化。情况如下:
我的用户表是:

+-------------------------+-----------------------+------+-----+---------+----------------+
    | Field                   | Type                  | Null | Key | Default | Extra          |
    +-------------------------+-----------------------+------+-----+---------+----------------+
    | id                      | mediumint(8) unsigned | NO   | PRI | NULL    | auto_increment |
    | ip_address              | varchar(45)           | NO   |     | NULL    |                |
    | username                | varchar(100)          | NO   |     | NULL    |                |
    | password                | varchar(80)           | NO   |     | NULL    |                |
    | salt                    | varchar(40)           | YES  |     | NULL    |                |
    | email                   | varchar(254)          | NO   | UNI | NULL    |                |
    | activation_code         | varchar(40)           | YES  |     | NULL    |                |
    | forgotten_password_code | varchar(40)           | YES  |     | NULL    |                |
    | forgotten_password_time | int(11) unsigned      | YES  |     | NULL    |                |
    | remember_code           | varchar(40)           | YES  |     | NULL    |                |
    | created_on              | int(11) unsigned      | NO   |     | NULL    |                |
    | last_login              | int(11) unsigned      | YES  |     | NULL    |                |
    | active                  | tinyint(1) unsigned   | YES  |     | NULL    |                |
    | first_name              | varchar(50)           | YES  |     | NULL    |                |
    | last_name               | varchar(50)           | YES  |     | NULL    |                |
    | company                 | varchar(100)          | YES  |     | NULL    |                |
    | phone                   | varchar(20)           | YES  |     | NULL    |                |
    +-------------------------+-----------------------+------+-----+---------+----------------+

我的组表是:

+-------------+-----------------------+------+-----+---------+----------------+
    | Field       | Type                  | Null | Key | Default | Extra          |
    +-------------+-----------------------+------+-----+---------+----------------+
    | id          | mediumint(8) unsigned | NO   | PRI | NULL    | auto_increment |
    | name        | varchar(20)           | NO   |     | NULL    |                |
    | description | varchar(100)          | NO   |     | NULL    |                |
    +-------------+-----------------------+------+-----+---------+----------------+

除此之外,我还有这样一种关系:

+----------+-----------------------+------+-----+---------+----------------+
    | Field    | Type                  | Null | Key | Default | Extra          |
    +----------+-----------------------+------+-----+---------+----------------+
    | id       | mediumint(8) unsigned | NO   | PRI | NULL    | auto_increment |
    | user_id  | mediumint(8) unsigned | NO   | MUL | NULL    |                |
    | group_id | mediumint(8) unsigned | NO   | MUL | NULL    |                |
    +----------+-----------------------+------+-----+---------+----------------+

好吧,在这种情况下,我想得到一个适当的用户(在这个例子中是user2)所属的所有组。
我的javascript代码是:

var mysql = require('promise-mysql');

var connection;
var arr = [];

mysql.createConnection({
    host: 'host',
    user: 'user',
    password: 'password',
    database: 'database'
}).then(
    function (conn) {
        connection = conn;
        return conn.query('select * from users_groups where user_id=2');
    }
).then(
    function(value) {
        console.log('Initial value : ' + JSON.stringify(value) );

        for (let user_group of value) {
            console.log('Each value : ' + JSON.stringify(user_group.group_id));
            arr.push( connection.query(`select * from groups where id = ${user_group.group_id}`));
        }     
        return arr;
    }, function (reason) {
        console.log('Rechazo: ' + reason);    
    }
).then (
    function (echo) {
        console.log('Echo: ' + JSON.stringify(echo));
        connection.end();
    }
);

我得到了:

Initial value : [{"id":10,"user_id":2,"group_id":1},{"id":11,"user_id":2,"group_id":2}]
Each value : 1
Each value : 2
Echo: [{"isFulfilled":false,"isRejected":false},{"isFulfilled":false,"isRejected":false}]

有人能帮我吗?提前谢谢!

6rvt4ljy

6rvt4ljy1#

为了让承诺链正常工作,你必须在你的工作中回报承诺 then 调用任何异步操作。在你的第二个 then 调用时,将一系列异步db调用推入一个数组。既然返回的是数组而不是承诺,那么下一个 then 调用不会等待所有查询完成。将此数组传递给 Promise.all 相反,你会得到你想要的结果。

var mysql = require('promise-mysql');

var connection;
var arr = [];

mysql.createConnection({
    host: 'host',
    user: 'user',
    password: 'password',
    database: 'database'
}).then(
    function (conn) {
        connection = conn;
        return conn.query('select * from users_groups where user_id=2');
    }
).then(
    function(value) {
        console.log('Initial value : ' + JSON.stringify(value) );

        for (let user_group of value) {
            console.log('Each value : ' + JSON.stringify(user_group.group_id));
            arr.push( connection.query(`select * from groups where id = ${user_group.group_id}`));
        }     
        return Promise.all(arr);
    }, function (reason) {
        console.log('Rechazo: ' + reason);    
    }
).then (
    function (echo) {
        console.log('Echo: ' + JSON.stringify(echo));
        connection.end();
    }
);

相关问题