简体   繁体   中英

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. 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? 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)

test.jade

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

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});
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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