简体   繁体   English

更改 SQL 查询中的变量值

[英]Change value of variable inside SQL query

I hate to ask such a simple question but I can't find an answer anywhere.我讨厌问这样一个简单的问题,但我无法在任何地方找到答案。

I am trying to push the sql results to results2 , if I print out the results2 value inside of the connection query it shows the array but when I try to return or print it outside it shows as undefined.我正在尝试将 sql 结果推送到results2 ,如果我在连接查询中打印出results2值,它会显示数组,但是当我尝试返回或在外部打印时,它显示为未定义。

Code:代码:

function getUserData(cookie){
  const sqlquery = "SELECT * FROM `users` WHERE `cookie` = ?"
  const params = [cookie];

  let results2 = [];
  connection.query(sqlquery, params, function(error, results, fields){
    if(error) throw error;
    if(results && results.length){
      results2.push(results[0]);
    }else{
      return("invalid");
    }
  })
  console.log(results2)
  return(results2);
}

I have searched and read through documentations but I can't find where I am messing up.我已经搜索并通读了文档,但找不到我搞砸的地方。

Your issue is that JavaScript is synchronous (tries to do as much as it can at once) unless you tell it not to be.你的问题是 JavaScript 是同步的(试图同时做尽可能多的事情),除非你告诉它不是。 It is returning reults2 before it has actually populated it because it is faster for it to do it that way.它在实际填充之前返回reults2 ,因为这样做速度更快。 So you need to make the function as a whole asynchronous by adding async before function and adding await before connection.query AND before wherever you are calling getUserData (as in await getUserData(var1, var2) ).因此,您需要通过在function之前添加async并在connection.query之前和调用 getUserData 之前添加await来使 function 作为一个整体异步(如await getUserData(var1, var2) )。

An explanation of async functions and await is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function异步函数和等待的解释在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

You may start getting errors about functions not begin asynchronous, as async functions need to nest to work (anything that await s needs to be in an async function itself).您可能会开始收到有关函数未开始异步的错误,因为异步函数需要嵌套才能工作(任何await s 都需要在 async function 本身中)。

Welcome to the world of callbacks and async ops.欢迎来到回调和异步操作的世界。

I am trying to push the sql results to results2, if I print out the results2 value inside of the connection query it shows the array but when I try to return or print it outside it shows as undefined.我正在尝试将 sql 结果推送到 results2,如果我在连接查询中打印出 results2 值,它会显示数组,但是当我尝试返回或在外部打印时,它显示为未定义。

Here console.log(results2) runs first and in the callback, you are assigning values to results2.这里console.log(results2)首先运行,在回调中,您正在为 results2 赋值。 So, you are unable to see it.所以,你看不到它。

You can use await with the query:您可以将 await 与查询一起使用:

//const util = require('util');

function getUserData(cookie){
  const sqlquery = "SELECT * FROM `users` WHERE `cookie` = ?"
  const params = [cookie];

  let results2 = [];
  try {
  // if not able to use await promisfy it using 
  // const query  = util.promisify(conn.query).bind(connection);
  // const result = await query(sqlquery, params);
  const result = await connection.query(sqlquery, params);
  results2.push(result);
 } catch(e) {
   console.log(e);
   }

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

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