【发布时间】:2017-03-31 06:45:49
【问题描述】:
promise 的概念对我来说是非常新的(到目前为止,我使用的是 async.each 和 async.waterfall) 我想使用 Promise,但我现在卡住了。
我想从我的数据库中获取“标签”。 我为此有两个表:一个称为“标签”,其中包含每个标签(带有 ID),另一个称为“user_tags”,其中保存了每个用户名以及用户(用户名)创建并保存到“标签”中的标签的 ID '。
我可以将信息放入我的数据库中,但现在我想将其取出并注销(稍后我将显示它)
到目前为止,这是我的想法:
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]);
}
});
});
}
这是个好主意吗?如果我想对这种功能使用 Promise,我该怎么办? 在此先感谢您的帮助!!!
【问题讨论】:
标签: javascript mysql node.js promise