Javascript 环回中基于belongsTo关系的过滤
我想根据其父项Javascript 环回中基于belongsTo关系的过滤,javascript,loopbackjs,Javascript,Loopbackjs,我想根据其父项belongsTo关系过滤模型 例如,我有一个Customer模型和一个Books模型 一个客户有很多书,一个客户的书,每个表都可能相当大 我想获得一个独特书籍的列表,其中拥有客户的名称为John 到目前为止,我知道我可以从书本模型出发,使用如下查询: Book.find({ include: { relation: 'customer', scope: { where: {name: 'John'}
belongsTo
关系过滤模型
例如,我有一个Customer
模型和一个Books
模型
一个客户有很多书
,一个客户的书
,每个表都可能相当大
我想获得一个独特书籍的列表,其中拥有客户的名称为John
到目前为止,我知道我可以从书本模型出发,使用如下查询:
Book.find({
include: {
relation: 'customer',
scope: {
where: {name: 'John'}
}
}
}, function(err, books) {
// Loop through the books here
});
或者,我可以从客户模型着手,手动合并图书列表(删除重复项):
我更喜欢第一种方法,因为它似乎可以在查询端执行复制逻辑
然而,这种方法似乎包括了所有书籍,但只有客户名为John的书籍才会添加到customer
属性中
我是不是走错了路?遗憾的是,这还没有完全实现。你们可以跟踪github问题的进展。是的,经过进一步的研究,我意识到loopback还没有实现内部连接,所以现在必须手动完成。遗憾的是,这还没有完全实现。您可以跟踪github问题的进展。是的,经过进一步的研究,我意识到loopback还没有实现内部连接,所以现在必须手动完成。
Customer.find({
where: {name: 'John'}
}, function(err, customers) {
var books = customers.map(customer => customer.books);
// Remove duplicates here
});