MySQL NodeJs - 让 rows.each 工作的正确方法 [英] MySQL NodeJs - Proper way to get rows.each to work

查看:59
本文介绍了MySQL NodeJs - 让 rows.each 工作的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我寻找简单的例子时,每个人的风格似乎都大不相同.我尝试了 2 种不同的风格,并遇到了 2 种不同的问题.在下面的代码中,我确定了代码的来源以及它在注释中出现的错误.我注释掉或取消注释掉每个部分并单独运行,但每个部分都有自己的错误.console.log(rows);"语句显示数据,因此查询本身正在运行和工作.

When I look for simple examples, everybody's style seems quite different. I'm tried 2 different styles, and got 2 different issues. In the code below, I have identified the source of the code and the error it gets in comments. I comment out or uncomment out each section and run separately, but each one has it's own errors. The "console.log(rows); " statement is showing the data, so the query itself is running and working.

// get the client
const mysql = require('mysql2');
const dashline = '---------------------------------';  

console.log (dashline); 
console.log ("Starting test"); 

// create the connection to database
const connection = mysql.createConnection({
  host: 'myhost',
  user: 'myuser',
  password: 'mypass',
  database: 'myDatabase'

});


console.log ("Got the database connection"); 


query = "select ID, user_nicename, user_email from wp_users where user_login = 'admin' limit 3 ";

console.log ("Starting query"); 

// Attempt 1
/*
connection.query(query, function(err, rows, fields){
  if (err) throw err;

  // from: https://html5hive.org/node-js-quickies-working-with-mysql/ 
  // error: SyntaxError: Unexpected token { (on the rows.each line below) 
  rows.each(element, index) {
    console.log(element.ID+ " " + element.user_nicename);
  }
  console.log (dashline); 
  console.log ("Query End"); 
  process.exit();   // Else Node hangs and must hit cntl-break to exit 

});
*/




// Attempt 2 

connection.query(query, function(err, rows, fields){
  if (err) throw err;
  console.log(rows); 
  // Roughly based on example on this page: 
  // https://datatables.net/reference/api/each()
  // TypeError: rows.each is not a function 
  rows.each( function(element, index) {
    console.log(element.ID + " " + element.user_nicename);
  });
  console.log (dashline); 
  console.log ("The end"); 
  process.exit();   // Else Node hangs and must hit cntl-break to exit 
});

推荐答案

在这里得到了答案:[https://github.com/sidorares/node-mysql2/issues/999[1]..forEach、.each 或 .map 的问题在于您位于另一个不是异步函数的函数中,这意味着您不能使用await"来调用另一个异步例程.

Got an answer here: [https://github.com/sidorares/node-mysql2/issues/999[1]. Problem with .forEach, .each or .map is that you are inside another function which is not an async function, meaning you cannot use "await" to call another async routine.

for (let r=0; r < rows.length; ++r) {
  console.log(rows[r].ID + " " + rows[r].user_nicename);
  await UpdatePassword(connection, rows[r].ID);
}

他还提供了这个替代方案:

He also provided this alternative:

一种类似于 map 的功能性"顺序迭代方式(imo 可读性稍低于 for 循环):

One "functional" way to iterate sequentially similar to map ( imo slightly less readable then for loop ):

await rows.reduce( async (previousPromise, row) => {
  await previousPromise;
  return UpdatePassword(row.ID);
}, Promise.resolve());

这篇关于MySQL NodeJs - 让 rows.each 工作的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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