I have the following Schemas for tracking Habits, and a user's progress
HABIT SCHEMA
const HabitSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
title: {
type: String,
required: true,
},
type: {
type: String,
enum: ["Goal", "Limit"],
default: "Goal",
required: true,
},
dateStarted: {
type: Date,
default: Date.now,
required: true,
},
});
PROGRESS SCHEMA: each time a user clicks that they completed a habit for a given period (day, week, month etc) a document will be created for that period, to indicate that it was done.
const ProgressSchema = new mongoose.Schema({
habit: {
type: mongoose.Schema.Types.ObjectId,
ref: "Habit",
},
period: {
type: Date,
default: Date.now,
required: true,
},
value: {
type: Number,
default: 1,
required: true,
},
});
When I get all of a user's habits, I want to also get all progress documents per habit. I want to join these objects before returning.
Here's what I have attempted:
let habits = await Habit.find({ user: req.user.id }).sort({
dateStarted: -1,
});
habits = await Promise.all(
habits.map(async (h, i) => {
try {
const progress = await Progress.find({ habit: h._id });
return { ...h, progress };
} catch (e) {
res.status(500).send("Server Error");
}
})
);
res.json({ habits });
This gives the error: CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Habit"
What would be the correct way to accomplish this?