简体   繁体   English

如何在查询加载后让 mysql nodejs 发送变量

[英]how to get mysql nodejs to send variable after query loads

I have looked up promises but cant figure it out, and i dont understand them.我查过promise,但无法弄清楚,我不明白它们。 I know everything else works and sends data to the client, just empty.我知道其他一切正常并将数据发送到客户端,只是空的。 and its because i cant assign the variable in time when sql finished?这是因为我无法在 sql 完成时及时分配变量?

app.post('/api', urlencodedParser,function(req, res,next) {

  console.log(req.body);
  var data = req.body.productObjs;
var bool;


  data.forEach(function(product) {

    connection.query(`SELECT * FROM productInfo where specId = ${product.productSpec}`,function(error,rows){
      
(error,rows)=>{

      if (error) {
        next('error making the query: ' + error.message)
      }
       else {
        console.log(rows[0]['specId'])
        console.log(rows[0]['productPrice'])
        if (product.productPrice === rows[0]['productPrice']) {
      bool=true
        }
        if (product.productPrice !== rows[0]['productPrice']) {
bool=false
        }
      }
    })

});
 //res.send({"bool":bool})
})
})

So the very first clear issue is that you have a callback within a callback.所以第一个明确的问题是你在回调中有一个回调。 That'd be my first refactoring attempt:那将是我的第一次重构尝试:

app.post('/api', urlencodedParser, function (req, res, next) {
  console.log(req.body);
  const data = req.body.productObjs;
  let bool;

  data.forEach((product) => {
    connection.query(`SELECT * FROM productInfo where specId = ${product.productSpec}`, (error, rows) => {
      if (error) {
        next('error making the query: ' + error.message);
      } else {
        console.log(rows[0]['specId']);
        console.log(rows[0]['productPrice']);
        bool = product.productPrice === rows[0]['productPrice'];
      }
    });
  });
});
  1. I're removed double callbacks我被删除了双重回调
  2. Simplified condition to assign your value to bool variable将值分配给bool变量的简化条件
  3. Changed types for modern JS syntax更改了现代 JS 语法的类型

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

相关问题 如何从nodeJS的查询块中获取变量? - How to get a variable out of the query block in nodeJS? 如何在不使用 NodeJS 加载页面的情况下发送 POST 请求并获取响应 - how to send POST request and get back response without the page loads with NodeJS 如何在NodeJS中的MySQL查询中增加外部变量值 - How to increment external variable value within MySQL Query in NodeJS 页面加载后如何设置带有mysql查询结果的单选按钮值后如何触发javascript函数 - How to fire a javascript function after setting a radio button value with a mysql query result when the page loads NodeJS MySQL 如何获取查询之外的结果 function - NodeJS MySQL How to get the result outside of the query function NodeJS-如何通过执行的查询获取mysql结果? - NodeJS - how can get mysql result with executed query? 如何在 nodejs 中获取带有 mysql 的查询方法的结果? - How can I get the result of a query method with mysql in nodejs? 页面加载后是否可以让PHPmailer发送邮件? - Is it possible to get PHPmailer to send the mail after the page loads? 如何使用nodejs运行sql查询后获取返回的函数 - how to get a function to return after running the sql query with nodejs 如何在另一个函数(NodeJS mysql)的查询MySQL中使用“结果”变量(回调函数) - How to use “results” variable (callback function) inside query mysql in another function (Nodejs mysql)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM