I'm trying to make an app with MERN stack. When I'm trying to add a user to the current database, it returns with CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Project"
What have I done wrong?
API.js
import axios from "axios";
export default {
updateUser: function (id, data) {
return axios.put("/api/user/" + id, data);
},
}
apiRoute.js
app.put("/api/project/:id/add-user", async (req, res) => {
// Find the project that was created and update it with a user
// console.log("Hello")
try {
const dbProject = await db.Project.findOneAndUpdate({ _id: req.params.id }, {
// Append the User to the Project object
$push: { users: req.body.userId }
}, { new: true });
// Send the request back to the front end
res.send(dbProject)
} catch (error) {
console.log({ "PUT - Project Add User": error })
res.send(error)
}
});
project.js(model)
const mongoose = require('mongoose');
const { Schema } = mongoose;
const projectSchema = new Schema({
project_name: {
type: String,
unique: true
},
team_lead: String,
description: String,
tags: String,
location: String,
num_members: Number,
// image: String,
users: [
{
type: Schema.Types.ObjectId,
ref: "users"
}
]
});
const Project = mongoose.model('Project', projectSchema);
module.exports = Project;
Full error msg:
{
[0] 'PUT - Project Add User': CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Project"
[0] at model.Query.exec (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:4358:21)
[0] at model.Query.Query.then (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:4450:15)
[0] at processTicksAndRejections (node:internal/process/task_queues:93:5) {
[0] messageFormat: undefined,
[0] stringValue: '"undefined"',
[0] kind: 'ObjectId',
[0] value: 'undefined',
[0] path: '_id',
[0] reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
[0] at new ObjectID (/Users/tolit/Downloads/Co-Lab-master/node_modules/bson/lib/bson/objectid.js:59:11)
[0] at castObjectId (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/cast/objectid.js:25:12)
[0] at ObjectId.cast (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/schema/objectid.js:267:12)
[0] at ObjectId.SchemaType.applySetters (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/schematype.js:1075:12)
[0] at ObjectId.SchemaType._castForQuery (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/schematype.js:1510:15)
[0] at ObjectId.SchemaType.castForQuery (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/schematype.js:1500:15)
[0] at ObjectId.SchemaType.castForQueryWrapper (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/schematype.js:1477:20)
[0] at cast (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/cast.js:331:32)
[0] at model.Query.Query.cast (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:4759:12)
[0] at castQuery (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:4559:18)
[0] at model.Query.Query._findAndModify (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:3458:23)
[0] at model.Query.<anonymous> (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/query.js:3028:8)
[0] at model.Query._wrappedThunk [as _findOneAndUpdate] (/Users/tolit/Downloads/Co-Lab-master/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
[0] at /Users/tolit/Downloads/Co-Lab-master/node_modules/kareem/index.js:369:33
[0] at processTicksAndRejections (node:internal/process/task_queues:75:11)
[0] }
[0] }