异步的NodeJS数据库功能需要同步答案 [英] Nodejs asynchronous database function needs synchronous answer

查看:211
本文介绍了异步的NodeJS数据库功能需要同步答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的NodeJS和我写一些code需要查询我的MySQL数据库,并从给定USER_ID返回一个用户名。我一直在读你的所有的功能应该是异步的。在这种情况下,理想情况下,我想在服务器能够同时这个查询正在发生响应其它事件的请求。然而,这不是一个特别大的查询,并仅返回一个值。也许我应该让同步? (如果这是你的答案,样品code改变将是巨大的)不管怎么说,这里是我的功能。它给人的最后一行近一个错误返回current_username;因为current_username在这一点上不确定的。有什么建议?

I am new to nodejs and am writing some code that needs to query my MySQL database and return a username from a given user_id. I've been reading that all your functions should be asynchronous. In this case, ideally I would like the server to be able to respond to other event requests while this query is taking place. However, it isn't a particularly large query and only returns a single value. Maybe I should make it synchronous? (If that is your answer, sample code to change it would be great) Anyways, here is my function. It gives an error near the last line "return current_username;" because current_username is undefined at that point. Any suggestions?

function get_current_username(current_user_id) {
    console.log(' | Entered get_current_username');
    sqlq = 'SELECT username FROM users WHERE id = ' + current_user_id;
    connection.query(sqlq, function(err, rows, fields) {
      if (err) throw err;
      var current_username = rows[0].username;
      console.log(' | the current_username =' + current_username);        
    });

return current_username;

}

推荐答案

在回调函数传递给 get_current_username ,然后调用回调函数从<$ C $内C> connect.query :

Pass in a callback function to get_current_username and then call that callback function from inside of connect.query's callback:

function get_current_username(current_user_id, callback) {
    console.log(' | Entered get_current_username');
    sqlq = 'SELECT username FROM users WHERE id = ' + current_user_id;
    connection.query(sqlq, function(err, rows, fields) {
      if (err) throw err;
      var current_username = rows[0].username;
      console.log(' | the current_username =' + current_username);        
      callback(current_username);
    });
}

当你去使用此功能的话,你会做这样的事情:

When you go to use this function then, you'd do something like:

get_current_username(12345, function(username) {
   console.log("I am " + username);
});

您还可以检查出使用承诺/期货。我有一种感觉,我就无法做这些司法解释,所以我会关闭链接到此的 StackOverflow的问题大概了解承诺的。

You could also check out the use of promises/futures. I have a feeling I won't be able to do an explanation of these justice, so I'll link off to this StackOverflow question about understanding Promises.

这是一个虽然架构决策 - 有些人会preFER使用回调,特别是如果写专供第三方重复使用的模块。 (事实上​​,它可能是最好采取类似的承诺之前,让你的头在这里学习阶段回调周围完全包裹。)

This is an architectural decision though - some people would prefer to use callbacks, especially if writing a module intended for re-use by 3rd parties. (And in fact, it's probably best to get your head fully wrapped around callbacks in the learning stages here, before adopting something like Promise.)

这篇关于异步的NodeJS数据库功能需要同步答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆