获取与模型相关的所有收集记录

时间:2015-06-16 14:50:57

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

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

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

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

P.S。我确实在数据库中有记录,并且正确设置了ER关系,因为插入Note记录没有问题。

1 个答案:

答案 0 :(得分:2)

模特关系似乎很好。

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

请改为尝试:

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