Banged my head against the wall for past 3-4 hours and checked countless articles here on StackOverflow but could not get my response to populate an array correctly. Using Express.js with Typescript, MongoDB and mongoose. Issue is when I get a response with all the orders my orderedlines array is empty even though I can check and see the ids are there in MongoDB atlas. Here is the actual response:
[
{
"orderedlines": [],
"_id": "6251c61f7385c349f88fe95a",
"userId": {
"favourites": [
"623b39e684b9baf1109053f8",
"623b3afada0e7828602c78df",
"623b3b49da0e7828602c78e7",
"623b39ba84b9baf1109053f7",
"623b3b59da0e7828602c78e9"
],
"_id": "62326179b9c85d3fc833d686",
"orders": [],
"email": "testche_emailche@gmail.com",
"username": "stef1222",
"password": "$2b$10$3e5Y/IoyrcJHH3ud6Mn/I.8PfBm2JrEKHwYRd8cQwUaAdz.YkKSMa",
"firstName": "Stefan",
"lastName": "Georgiev",
"image": "https://res.cloudinary.com/dtggdx3hc/image/upload/v1648046254/deqr4chfysogoppafdug.png",
"isAdmin": false,
"hasWriteAccess": false,
"__v": 0
},
"totalPrice": 121.99,
"__v": 0
}
]
As seen above my userId is being populated successfully with all its properties but orderedlines is failing to populate and it is returned as empty array. If I remove the .populate() it returns an array of objects with ids
My findOrdersForUserId function in orderServices where I assume the problem occurs
const findOrdersForUserId = async (
userId: string
): Promise<OrderDocument[]> => {
const ordersToReturn = await Order.find({ userId: userId })
.sort({ _id: 1 })
.populate('userId')
.populate({path:'orderedlines', model:OrderLine})
return ordersToReturn
}
Here is my Order model:
import mongoose, { Document } from 'mongoose'
export type OrderLine = {
orderlineId: string
}
export type OrderDocument = Document & {
userId: string
orderedlines: OrderLine[]
totalPrice: number
}
const orderSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
totalPrice: Number,
orderedlines: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'OrderLine',
},
],
})
export default mongoose.model<OrderDocument>('Order', orderSchema, 'orders')
My OrderLine model:
import mongoose, { Document } from 'mongoose'
export type OrderLineDocument = Document & {
productId: string
userId: string
quantity: number
price: number
}
const orderLineSchema = new mongoose.Schema({
quantity: { type: Number, default: 1 },
price: Number,
productId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product',
required: true,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
})
export default mongoose.model<OrderLineDocument>('OrderLine', orderLineSchema, 'orderlines')
Mongoose version used: mongoose@5.13.10
Node version: v16.13.0
Mongo Shell version: v5.0.6
Express version: express@4.17.1
I would list some of the articles I tried to fix my the issue without success:
- Mongoose populate returning empty array
- Mongoose populate() returning empty array
- Mongoose populate() returns empty array with no errors
- Mongoose Populate not working with Array of ObjectIds
- Populate method not populating my comment array
- Mongoose populate does not populate array
- Mongoose populate not populating an array and always returns an empty array
Edit: All the code is a part of a fork from a repo that is set to private. I am not sure how can I share access/link to the fork for a better preview of the code. Please enlighten me if you would like to see something else or a more specific part of the code.