Reputation: 2625
In my webapp, after ordering (SingleOrder) for products, the customer should check for offers. if available, then I should add the order to the ComboOfferOrder.
There, I want to check for the order's payment status. Also, I have to get the entire products list.
I have all the values in my db in backend. But I am not able to populate any of the objects in 'SingleOrder' for my api method.
I have the below schemas.
*User*
{
name : String,
email : Sring,
role : String
}
*BankTransaction*
{
type : String,
transationReference: String,
date : Date,
amount : Number
}
*ComboOfferOrder*
{
customer :{
type :Schema.ObjectId,
ref : 'User'
},
order : {
type :Schema.ObjectId,
ref : 'SingleOrder'
},
productList : [{
type :Schema.ObjectId,
ref : 'Product'
}]
discount : Number,
totalCost : Number,
paymentStatus :String,
deliveryStatus : String
}
*SingleOrder*
{
code: String,
products : {
groceries:[{
type :Schema.ObjectId,
ref : 'Product'
}],
other:[{
type :Schema.ObjectId,
ref : 'Product'
}]
},
billingAddress: String,
deliveryAddress : String,
payment:{
status : String,
transaction :{
type :Schema.ObjectId,
ref : 'BankTransaction'
}
}
}
*Products*
{
name : String,
cost : Number,
expiryDate : Date
}
My api
mongoose.model('ComboOfferOrder')
.findOne({
_id: comboOfferOrderId
})
.select('order')
.exec(function(err, comboOfferOrder) {
var paths = [
{path : "payment.status"},
{path : "payment.trasaction"},
{path : "products.groceries"},
{path : "products.other"}
];
mongoose.model('comboOfferOrder').populate(comboOfferOrder.order,paths,function(err, singleOrder) {
if (err) {
return deferred.reject(err);
}
return deferred.resolve(comboOfferOrder.order);
});
});
In the result, I get only the objectIds of "payment.status","payment.trasaction",products.groceries", "products.other"
Please let me know the solution.Thanks.
Upvotes: 1
Views: 1336
Reputation: 11
Populate lets you get a list of a user's friends, but what if you also wanted a user's friends of friends? Specify the populate option to tell mongoose to populate the friends array of all the user's friends:
User.
findOne({ name: 'Val' }).
populate({
path: 'friends',
// Get friends of friends - populate the 'friends' array for every friend
populate: { path: 'friends' }
});
Upvotes: 1
Reputation: 4029
You can't populate a nested field with mongoose, and therefore you must nest your callbacks.
You may reread the documentation of populate for usage examples.
This should work (not tested):
mongoose.model('ComboOfferOrder')
.findOne({
_id: comboOfferOrderId
})
.select('order')
.exec(function(err, comboOfferOrder) {
comboOfferOrder.populate('order', function(err, singleOrder) {
singleOrder.populate('products.other products.groceries etc...', function(err, singleOrder) {
if (err) {
return deferred.reject(err);
}
return deferred.resolve(singleOrder);
});
});
});
Upvotes: 1