在下面的代码中,我试图向mainQuery对象中添加populate。如果我只传递types,那么我会得到预期的结果并且查询也会构建成功。但是如果我同时传递types和子类别,则查询将会中断,并且对象不会按照下面的预期查询添加。请给出解决方案?
mainQuery = Category.find();
if(req.param('types')) {
searchKey = "types";
typesObj.where = {
id: 1
};
mainQuery.populate(searchKey, typesObj);
}
if(req.param('subcat')) {
searchkeySubCat = "subcategory";
typesSubcat.where = {
id: 1
};
mainQuery.populate(searchkeySubCat, typesSubcat);
}
mainQuery.exec(function (err, category) {
});
期望的查询如下
Category.find().populate('types', {where: {id:1}}).populate('subcategory', {where: {id:1}}).exec(function (err, res) {
console.log(res)
})