【问题标题】:Node js and mysql return query resultnode js和mysql返回查询结果
【发布时间】:2020-06-20 18:49:57
【问题描述】:

你好我是新学习nodejs并连接mysql数据库 现在我想将选择查询的结果保存在某种类型的变量中,但我不能。

var campGround = [];


console.log("Select Statement started....");
    con.connect(function(error){

        if(!error){
            console.log("Connected");
            var sql = "select * from campgrounds";
            con.query(sql,function(err,result,field){
                if (!err) {
                    // console.log(JSON.parse(result));
                    for(var i =0;i<result.length;i++)
                    {
                        try {
                            // console.log(result[i]);
                            setCampground(result[i]);
                            // campGround.push(result[i]);
                        } catch (error) {
                            console.log(error.message);
                        }

                    }
                }
                else{
                    console.log("Error while selecting record from campground table. ");
                }
            });
        }else{

            console.log("Error DataBase Not Connected!!! select statement");
        }
    });

    function setCampground(value){
        this.campGround.push(value);
    }


    console.log("length after execution :: "+campGround.length);
    campGround.forEach(function(value){
        console.log("Campground array");
        console.log(value);
    });

当我执行上面的代码并对其进行调试时……select 语句从数据库中返回 3 条记录……但是当我将它们推入数组时……并打印数组时没有任何反应……请帮助

我找不到任何可以帮助我的东西。

【问题讨论】:

    标签: mysql node.js visual-studio-code node.js-connect


    【解决方案1】:

    您的 mysql 查询调用是异步的(基于回调),并且您记录 campGround 的调用在该回调之外,因此这意味着您正在拨打电话但不等待该调用完成。这就是你的 campGround 没有打印任何东西的原因。

    您需要在处理错误和响应的回调中移动以下行。像这样的

    const campGround = [];
    
    
    console.log("Select Statement started....");
    con.connect(function (error) {
    
      if (!error) {
        console.log("Connected");
        const sql = "select * from campgrounds";
        con.query(sql, function (err, result, field) {
          if (!err) {
            // console.log(JSON.parse(result));
            for (let i = 0; i < result.length; i++) {
              try {
                // console.log(result[i]);
                setCampground(result[i]);
                // campGround.push(result[i]);
              } catch (error) {
                console.log(error.message);
              }
    
            }
          } else {
            console.log("Error while selecting record from campground table. ");
          }
          console.log(`length after execution :: ${campGround.length}`);
          campGround.forEach(function (value) {
            console.log("Campground array");
            console.log(value);
          });
        });
      } else {
    
        console.log("Error DataBase Not Connected!!! select statement");
      }
    });
    
    function setCampground(value) {
      this.campGround.push(value);
    }
    
    

    【讨论】:

      【解决方案2】:

      你有:

      const campGround = [];
      

      即全局变量(或模块范围);

      然后在代码中你有一个函数

      function setCampground(value) {
        this.campGround.push(value);
      }
      

      也属于全局范围(或​​模块范围) 因此this.campGround 不是campGround

      this.campGround .push(value); 更改为campGround .push(value);,现在一切正常。

      【讨论】:

        猜你喜欢
        • 2015-07-15
        • 2020-02-03
        • 2018-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-24
        • 2013-11-07
        • 1970-01-01
        相关资源
        最近更新 更多