I've been struggling for the past 3-4 hours, banging my head against the wall and scouring countless articles here on StackOverflow, but I just can't seem to get my response to populate an array correctly. I'm working with Express.js, Typescript, MongoDB, and mongoose. The issue arises when I receive a response containing all the orders; my orderedlines array remains empty despite confirming that the ids are present in MongoDB atlas. Here is the actual response:
[
{
"orderedlines": [],
"_id": "6251c61f7385c349f88fe95a",
"userId": {
"favourites": [
"623b39e684b9baf1109053f8",
"623b3afada0e7828602c78df",
"623b3b49da0e7828602c78e7",
"623b39ba84b9baf1109053f7",
"623b3b59da0e7828602c78e9"
],
"_id": "62326179b9c85d3fc833d686",
"orders": [],
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b2a3b5b2a5aea399a3aba7afaaa5aea386a1aba7afaae8a5a9ab">[email protected]</a>",
"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
}
]
Despite successfully populating my userId with all its properties, the orderedlines field fails to populate and is returned as an empty array. Removing .populate() results in an array of objects with ids.
The problem likely lies in my findOrdersForUserId function in orderServices:
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: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4898b8a838b8b9781a4d1cad5d7Cad5D4">[email protected]</a>
Node version: v16.13.0
Mongo Shell version: v5.0.6
Express version: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46233e363423353506726877716877">[email protected]</a>
I've attempted various articles to resolve 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.