简体   繁体   English

从承诺中选择MySQL node.js中的多个数据

[英]SELECT multiple data from MySQL node.js with promises

The concept of promises is very new for me (so far, I was working with async.each and async.waterfall) I want to use promises but i'm stuck right now. 对我来说,promise的概念非常新(到目前为止,我正在使用async.each和async.waterfall进行操作)。我想使用Promise,但是我现在仍处于困境。

I want to get "tags" from my db. 我想从数据库中获取“标签”。 I have two tables for this : One called 'tags' with every tag in it (with an ID) and another one 'user_tags' with every username saved and the ID of the tag that the user (username) created and saved into 'tags'. 为此,我有两个表:一个名为“ tags”,其中包含每个标签(带有ID),另一个名为“ user_tags”,其中包含每个用户名的保存以及用户(用户名)创建并保存到“ tags”中的标签的ID '。

I can put information in my DB but now I want to pull this out and log it out (I will display it later) 我可以将信息放入数据库中,但是现在我想将其提取并注销(稍后将显示)

So far this is my idea : 到目前为止,这是我的想法:

var getUserprofile = function getUserprofile(username, callback){
  pool.getConnection(function (err, connection) {
    var dataUser = [];

    // Error check
    if (err) {
      console.log(err);
    }

    connection.query('SELECT * FROM users_tags FULL JOIN tags ON (tags.id = users_tags.t_id) WHERE users_tags.user_id=666;', username , function (err, rows, fields) {
      if (err) {
        connection.release();
        cb(err);
      } else if (rows.length < 1) {
        connection.release();
        cb("We don't have any informations about this user yet");

      } else {
        console.log("we pull the information right now");
        connection.release();
        callback(null, rows[0]);
      }
    });
  });
}

Is this a good idea ? 这是一个好主意吗 ? What should I do if I want to use promises for this kind of function ? 如果我想对此类功能使用promise,该怎么办? Thanks in advance for any help !!! 在此先感谢您的帮助!

I would use Bluebird . 我会使用Bluebird You can "promisify" existing APIs with Promise.promisify or Promise.promisifyAll . 您可以使用Promise.promisifyPromise.promisifyAllPromisise现有的API。

I would do something like 我会做类似的事情

var Promise = require('bluebird'),
     ... //other deps;

var pool = Promise.promisifyAll(pool);
function getUserprofile(username){
    var connection = null;
    return pool.getConnectionAsync()
        .then(function (conn) {
             connection = Promise.promisifyAll(conn);
             return connection.queryAsync('...');
        })
        .then(function (results) {
             if (results.length < 1) {
                 return "We don't have any informations about this user yet";
             } else {
                 console.log("we pull the information right now");
                 return results[0];
             }
        })
        .catch(function (err) {
             console.log(err);
             throw err;
        })
        .finally(function () {
             if (connection) {
                 connection.release();
             }
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM