I am new to node.js, I haven’t been able to find an answer that fixes my problem. What I’m trying to do is create a path that will create a new child object, add it to the parent’s array of children, then return the child object to the requester.The problem that I am running into is that if I pass the string id into findById, node crashes.
CONSOLE ERROR:CastError: Cast to ObjectId failed for value "{ role: 'manager' }" (type Object) at path "_id" for model "user"
CODE SNIPPET: `app.route("/select").get( async (req, res, next) => {
const {manager} = req.body;
try {
const findMangers = await User.findById({role: "manager"})
if (findMangers) {
return res.json(findMangers);
} else if (findMangers.length === 0) {
return res.json('0 managers')
} else {
return res.json("Oops! an error occurr")
}
} catch (e) {
return next(e);
}
});
`
USERMODEL:`const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema({
username:{
type: String
},
manager: {
type: Schema.Types.ObjectId,
ref: "user"
},
email:{
type: String,
required: true
},
password:{
type: String,
required: true
},
role:{
type: String,
enum: ["manager", "staff"],
default: "staff"
},
deleted: {
type: Boolean,
default: false
},
whenCreated:{
type: Date,
default: Date.now()
}
});
const modelUserSchema = mongoose.model("user", userSchema, "Users");
module.exports = modelUserSchema;`
Bsically a user should select manager from the listMager who have register . in other to send them message