简体   繁体   English

node.js从视图查询mysql数据库可能吗?

[英]node.js query mysql database from view possible?

In my node.js project I installed mysql and made this DB.js that queries the database. 在我的node.js项目中,我安装了mysql并制成了用于查询数据库的DB.js。 I'm stumped on how exactly would I query the database from the jade view. 我很困惑如何从玉视图中查询数据库。

To clarify my question, in .NET I would've returned an IQueryable from repositories and then expand the query inside the ASP.NET MVC controller, but what's the correspondent of that practice here in node? 为了澄清我的问题,在.NET中,我将从存储库中返回IQueryable,然后在ASP.NET MVC控制器内扩展查询,但是在Node中,这种做法的对应之处是什么? Is there even such a concept of a controller? 甚至还有控制器的概念吗?

So how to query the database and return only what you need in the view, while also not exposing "too much" of the database in the view itself? 因此,如何查询数据库并仅返回视图中所需的内容,同时又不在视图本身中暴露过多的数据库呢?

If you want to pass data from MySQL to a jade view in express (your question does not mention express but it is probably express) 如果您想将数据从MySQL传递到Express中的Jade视图中(您的问题没有提到Express,但可能是Express)

test.jade test.jade

table
  tr
    th Name
    th Value
  - each x in data
    tr
      td #{x.name}
      td #{x.value}

NodeJS: 的NodeJS:

app.get("/getViews/:price", function(req,res){
  mysql.query("SELECT name, value FROM mytable WHERE price > ?", [price], function(err,fields,rows){
    res.render("test", {data: rows});
  });
});

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

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