简体   繁体   English

Node.js-从SQL打印结果

[英]Node.js - print result from sql

I am trying to run a select query from db and print results. 我正在尝试从数据库运行选择查询并打印结果。 But even i see result in console i don't see in index page. 但是,即使我在控制台中看到结果,但在索引页面中却看不到。 (Hovewer i see result in console but it is also doesn't show correctly. I have 2 rows in db but i see 3 lines for each row. So result in console is : 2X3=6 rows.) I put screenshot about console result end of the question. (不过,我在控制台中看到结果,但是它也不能正确显示。我在db中有2行,但每行看到3行。所以在console中的结果是:2X3 = 6行。)问题的结尾。

Code in app.js app.js中的代码

app.use('/', routes, function(req, res){
    pg.connect(connect, function(err, client, done){
    if(err){
      return console.error('errrr', err)
    }
    client.query('select * from recipes', function(err, result){
    if(err){
      return console.error('error running query', err);
    }
     console.log(result.rows);
     res.render('index.njk', { recipes: result.rows});
     done(); 
    });
    }); 
    });

Code in index.njk index.njk中的代码

<ul>
  {% for name, item in recipes %}
  <li>{{ name }}: {{ item.name }}</li>
  {% endfor %}
</ul>

this is result of the console Can you please help me to fix it? 这是控制台的结果,您能帮我修复它吗?

It seems like (via your picture of the console) that result.rows is an array of arrays that has 2 values in it. 看起来(通过控制台的图片),result.rows是一个数组数组,其中有2个值。 Therefore, while looping over the value in your index.njk, recipes is an array of arrays and doesn't contain an item that has a name attribute. 因此,在遍历index.njk中的值时,配方是一个数组数组,并且不包含具有name属性的项。

If you set recipes: result.rows[0] that should provide a quick fix: 如果设置配方:result.rows [0],则应提供一个快速解决方案:

res.render('index.njk', { recipes: result.rows[0]});

This allows you to get the first element in your array of arrays, which is okay, because the array held within only has 1 element (the 2 items you actually want!). 这样就可以获取数组数组中的第一个元素,这没关系,因为其中包含的数组只有一个元素(实际上是您想要的2项!)。 You should check before you do this that result.rows.length > 0 so you don't go out of bounds and get an error. 在执行此操作之前,应先检查result.rows.length> 0,以免出现错误。

if(result.rows.length > 0) {
  res.render('index.njk', { recipes: result.rows[0]});
}else {
  console.log('No rows found in DB');
}

I have solved my issue by using below code block in index.js instead of using app.js. 我已经通过在index.js中使用以下代码块而不是使用app.js解决了我的问题。 I am not sure it is correct way but it is working fine now. 我不确定这是正确的方法,但现在工作正常。 If it is not correct way, let me correct it please. 如果方法不正确,请让我更正。

router.get('/', function(req, res){
  pg.connect(connect, function(err, client, done){
   if(err){
    return console.error('errrr', err)
    }
   client.query('select * from recipes', function(err, result){

    if(err){
      return console.error('error running query', err);
     }
    if(result.rows.length > 0) {
        res.render('index.njk', { recordResult: result.rows});
       console.log(result.rows);
     }else {
        console.log('No rows found in DB');
      }
    done() 
    });
  }); 
});

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

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