从sailsjs模型中的集合中检索特定的值

Merchant.js

module.exports = { attributes: { name:{ type:'string', required:true }, drinks:{ collection:'Drinks', via:'merchant' } } }; 

Drinks.js

 module.exports = { attributes: { name:{ type:'string', required:true }, category:{ model:'Category' }, merchant:{ model:'Merchant' } } }; 

Category.js

 module.exports = { attributes: { name:{ type:'string', required:true, unique: true } } }; 

我想要检索与给定的input类别相关的饮料的商家。 有人可以帮助我查找查询。

谢谢

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

$ in也不符合我的要求。 我没有find任何的帆支持。 所以我改变了我的逻辑,如下所示:

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