开发者

Use squelize find in jade

I am new to node.js / jade sequelize, the secnario i want to get to i开发者_如何学Gos, inside a jade file, can i do something like:

-Item.find( id ).on('success'), function(return) { p return.name p return.value })

Tried this a couple of times but couldn't get it working.

Thanks.


No.

That's not how views and templates work. We do not talk to the database in the view.

Before you render the view talk to the database.

Psuedo code:

Item.find(id).on("success", function(item) {
  res.render(view, {
    name: item.name,
    value: item.value
  });
});


this is not a sequelize-specific problem, but a jade/mvc one. First, following the mvc pattern, you should load all needed data in the controller and pass it afterwards to the view. Furthermore the problem you are experiencing exists due to Jade's incompatibility with asynchronous function calls. Because sequelize is working asynchronously, you won't be able to get your data in the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜