【问题标题】:Get all collection records related to a model获取与模型相关的所有集合记录
【发布时间】:2015-09-01 11:27:55
【问题描述】:
// Note model 
attributes: {
        // Relations
        notebook: {
            model: 'Notebook'
        },
}

// Notebook
attributes: {
        // Relations
        owner: {
            model: 'User'
        },

        notes: {
            collection: 'Note',
            via: 'notebook'
        }
}

在控制器中:

        Notebook.findOne({owner: user.id}, function (err, notebook) {
            if (err || !notebook) {
                return res.serverError(err);
            }
// --> until here it goes all fine, finding the Notebook
            Note.find().where({notebook: notebook.id}, function (err, notes) {
                if (err || !notes) {
                    return res.serverError(err);
                }

                return res.json({notebook: notebook, notes: notes});
            })
        })

很明显,我正在尝试获取与笔记本相关的所有笔记。调试时,我得到了Note.find(),然后我什至没有输入回调,所以我没有得到Note的任何结果。 err 为空,所以我不知道是否有问题。

我打赌我错误地设置了模型关系,但对我来说似乎是正确的,就像我在教程中读到的那样。

附:我确实在数据库中有记录,并且那里的 ER 关系设置正确,因为插入 Note 记录没有问题。

【问题讨论】:

    标签: javascript mysql node.js sails.js waterline


    【解决方案1】:

    模型关系似乎很好。

    我认为错误来自where method 中没有回调参数。

    试试这个:

    Note
      .find()
      .where({ notebook: notebook.id })
      .exec(function (err, notes) {
        ...
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 2019-02-24
      • 2015-07-26
      • 1970-01-01
      • 2017-06-18
      • 2018-05-31
      • 2016-07-10
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多