Javascript 获取与模型相关的所有集合记录
及 在控制器中:Javascript 获取与模型相关的所有集合记录,javascript,mysql,node.js,sails.js,waterline,Javascript,Mysql,Node.js,Sails.js,Waterline,及 在控制器中: // Notebook attributes: { // Relations owner: { model: 'User' }, notes: { collection: 'Note', via: 'notebook' } } 很明显,我正在尝试获取与笔记本相关的所有笔记。在调试时,我得到的是Note.find(),然后我甚
// Notebook
attributes: {
// Relations
owner: {
model: 'User'
},
notes: {
collection: 'Note',
via: 'notebook'
}
}
很明显,我正在尝试获取与笔记本相关的所有笔记。在调试时,我得到的是Note.find()
,然后我甚至没有进入回调,因此我没有得到Note
的任何结果。err
为空,因此我不知道是否有问题
我打赌我设置的模型关系是错误的,但从我在教程中读到的内容来看,我认为它是正确的
另外,我在数据库中确实有记录,并且ER关系设置正确,因为插入
注释记录不会出现问题。模型关系似乎很好
我认为这个错误是因为在中没有回调参数
请尝试以下方法:
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});
})
})
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()
.where({ notebook: notebook.id })
.exec(function (err, notes) {
...
});