获取某个模型相关的所有集合记录
Get all collection records related to a model
// 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
为空,所以我不知道是否有问题。
我打赌我错误地设置了模型关系,但从我在教程中读到的内容来看,它对我来说似乎是正确的。
P.S。我确实在数据库中有记录,并且那里的 ER 关系设置正确,因为插入 Note
记录没有问题。
模特关系好像还不错
我认为错误来自 where
method 中没有回调参数的事实。
试试这个:
Note
.find()
.where({ notebook: notebook.id })
.exec(function (err, notes) {
...
});
// 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
为空,所以我不知道是否有问题。
我打赌我错误地设置了模型关系,但从我在教程中读到的内容来看,它对我来说似乎是正确的。
P.S。我确实在数据库中有记录,并且那里的 ER 关系设置正确,因为插入 Note
记录没有问题。
模特关系好像还不错
我认为错误来自 where
method 中没有回调参数的事实。
试试这个:
Note
.find()
.where({ notebook: notebook.id })
.exec(function (err, notes) {
...
});