module.exports = {
attributes: {
name:{
type:'string',
required:true
},
drinks:{
collection:'Drinks',
via:'merchant'
}
}
};
module.exports = {
attributes: {
name:{
type:'string',
required:true
},
category:{
model:'Category'
},
merchant:{
model:'Merchant'
}
}
};
module.exports = {
attributes: {
name:{
type:'string',
required:true,
unique: true
}
}
};
我想要检查与给定输入类别相关的饮料的商家。 有人可以帮我查找查询。
由于
答案 0 :(得分:2)
var selectedCategory;//given input category.
async.waterfall([
function(callback) {
Drinks
.find({
category: selectedCategory
})
.exec(function(err, drinks) {
if (err) return callback(err);
callback(null, drinks);
});
},
function(drinks, callback) {
Merchant.find({
drinks: drinks //In Pairs
})
.populate('drinks')
.exec(function(err, merchants) {
if (err) return callback(err);
callback(null, merchants);
});
}
],
function(err, results) {
if (err) return res.badRequest(err);
res.ok(results);
});
答案 1 :(得分:0)
$ in也不符合我的要求。我没有找到任何风帆支持。 所以我改变了我的逻辑,如下所示:
var selectedCategory = req.param("value");
async.waterfall([
function(callback) {
Category
.findOne({
name: selectedCategory
})
.exec(function(err, cat) {
if (err) return callback(err);
callback(null, cat);
});
},
function(cat,callback) {
Drinks
.find({
category: cat.id
}).populateAll()
.exec(function(err, drinks) {
if (err) return callback(err);
callback(null, drinks);
});
},
function(drinks, callback) {
var merchantIds = [];// to store unique merchant ids
drinks.forEach(function (drink) {
if (merchantIds.indexOf(drink.merchant.id) < 0) {
merchantIds.push(drink.merchant.id);
}
})
callback(null, drinks,merchantIds);
},
function(drinks,merchantIds, callback) {
Merchant
.find({ where: {'id': {"$in": merchantIds}},skip:page*perPage,limit:perPage}).populateAll()
.exec(function (err, merchantList) {
if (err) return callback(err);
callback(null, drinks,merchantList);
});
},
function(drinks,merchantList, callback) {
var merchantsList = [];
merchantList.forEach(function (merchant) {
var merchants={};//to store merchant details in JSON format
merchants['id'] = merchant.id;
merchants['name'] = merchant.name;
merchants['drinks']=[]
drinks.forEach(function (drink) {
if (merchant.id===drink.merchant.id) {
merchants['drinks'].push(drink);
}
})
merchantsList.push(merchants)
})
callback(null, merchantsList);
}
],
function(err, results) {
if (err) return res.badRequest(err);
res.json(results);
});