Can someone please help me out, I'm trying to fetch the objects which contain matching email in their toDoEmail but the find method is returning empty array though I have objects with matching email.
Here is my model:
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const title = new schema({
title: {
type: String,
trim: true,
required: [true, "Title is required to create a ToDo"],
},
createdAt: { type: String, trim: true, default: new Date() },
updatedAt: { type: String, trim: true, default: new Date() },
});
const email = new schema({
email: {
type: String,
trim: true,
required: [true, "email is required to create a ToDo"],
},
createdAt: { type: String, trim: true, default: new Date() },
updatedAt: { type: String, trim: true, default: new Date() },
});
const tasks = new schema({
tasks: [
{
task: {
type: String,
trim: true,
required: [true, "Atleast one task is required to create a ToDo"],
},
status: { type: Boolean, trim: true, default: false },
createdAt: { type: String, trim: true, default: new Date() },
updatedAt: { type: String, trim: true, default: new Date() },
},
],
});
const status = new schema({
status: { type: Boolean, trim: true, default: false },
createdAt: { type: String, trim: true, default: new Date() },
updatedAt: { type: String, trim: true, default: new Date() },
});
const toDoSchema = new schema(
{
toDoTitle: title,
toDoTasks: tasks,
toDoStatus: status,
toDoEmail: email,
},
{
timestamps: true,
}
);
const toDoModel = mongoose.model("toDo", toDoSchema);
module.exports = toDoModel;
here is my route:
router.get("/fetchalltodos/:email", fetchAllToDos);
here is my controller:
//importing model
const toDoModel = require("../models/ToDoModel");
//creating controller function
const fetchAllToDosController = async (req, res) => {
try {
const { email} = req.params;
console.log(email)
const toDos = await toDoModel.find({ toDoEmail: {email:email} });
res.status(200).json({
success: true,
message: "updated successfully",
toDos,
});
} catch (err) {
res.status(401).json({
success: false,
message: "Failed to fetch todo's",
error: err.message,
});
console.log(err.message);
}
};
module.exports = fetchAllToDosController;
My database:
Server Response to postman:
my console.log of fetchalltodocontroller:
I have tried to console.log the email to check whether it is fetching the email from url params and it successfully console logged it. I'm not understanding why it is giving me empty array instead of collection of Objects which contain matching email.



your model is an Object, so can do filter with object too.
it could be: