I'm trying to search through the document TreatmentPlan for the subdocument in treatments that matches the clientId provided:
This is the TreatmentPlan model:
const treatmentPlanSchema = mongoose.Schema({
id: String,
clientId: String,
startDate: Date,
clientGoals: String,
objectivesOfTreatmentPlan: String,
conclusionOfTreatmentPlan: String,
endDate: Date,
treatments: [treatmentSchema]
})
This is the treatments subdocument:
const treatmentSchema = mongoose.Schema({
id: String,
clientId: String,
date: String,
time: String,
price: String
})
And this is the call I'm trying to make in Node:
export const getTreatmentsByClientId = async (req, res) => {
const { clientId: id } = req.params
try {
const result = await TreatmentPlan.find({clientId: id})
const treatments = result.treatments.find(({clientId})=>clientId === id)
console.log(treatments)
} catch (error) {
console.log(error.message)
}
}
When I run this, the error I get is "cannot read property 'find' of undefined."
I ran a similar function on a different array and it worked, but it doesn't seem to work for this array. Any ideas how to make this work?