Amc_rtty
Amc_rtty

Reputation: 3813

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?

Upvotes: 1

Views: 1708

Answers (1)

Mustafa
Mustafa

Reputation: 10413

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

Upvotes: 2

Related Questions