【问题标题】:Sent two models to a view with Sequelize使用 Sequelize 将两个模型发送到视图
【发布时间】:2013-02-14 07:38:17
【问题描述】:

我尝试使用 Sequelize 将两个模型发送到一个视图,但我不知道如何继续。

我下面的代码不起作用。

Post.findAll().success( function(posts) {
    Creation.findAll().success( function(creations) {
        res.render('admin_index', {
            creations: creations,
            posts: posts
        });
    });
});

安东尼

【问题讨论】:

  • 你遇到了什么问题?
  • 在我的视图“admin_index”中无法识别数组“posts”...

标签: node.js express sequelize.js


【解决方案1】:

实际上,您并没有在回调中返回任何帖子,这就是帖子未定义的原因。

这样您就无法在 yoru res.render 上下文中访问它。

检查这部分回调

 Creation.findAll().success( function(creations) {
    // The other stuff
});

在这里您只返回创作,而不是您应该编写一个返回创作和帖子的查询。或者像这样在回调链中执行多个查询。

 RandomQuery.findAll().success( function(creations,posts) {
    // The other stuff
});

或者将回调链接在一起

 Creation.findAll().success( function(creations) {
    Post.findAll().success(function(posts){

        res.render('admin_index', {
            creations: creations,
            posts: posts
        });  

    });
});

【讨论】:

  • 谢谢,它有效!我忘了切换“创作”和“帖子”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 2018-08-17
  • 2011-05-16
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多