Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I tried to clcik edit button its shows
Cast to ObjectId failed for value "{ _id: ' 630ce985474a211c98fe80c2' }" (type Object) at path "_id" for model "User"
?

What I have tried:

//models
const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({

name:{
type:String,
required:true
},
email:{
type:String,
required: true
},
mobile:{
type:String,
required:true
},
image:{
type:String,
required:true
},
password:{
type:String,
required:true
},
is_admin:{
type:Number,
required:true
},
is_verified:{
type:Number,
default:0
},
token:{
type:String,
default:''
}
})

module.exports = mongoose.model('User',userSchema);

//views
<% include('../layout/header.ejs') %>

<img src="userImage/<%=user.image  %> " width="100px" height="100px" alt="<%=user.image  %>">

<form action="" method="POST" enctype="multipart/form-data">
 <input type="file" name="image">
 <br><br>
 <input type="hidden" name="user_id" value="<%= user._id %>">
    <input type="text" name="name" value="<%= user.name %>" placeholder="Enter Your Name" required>
    <br><br>
    <input type="text" name="email" value="<%= user.email %>" placeholder="Enter Your Email" required>
    <br><br>
    <input type="text" name="mno" value="<%= user.mobile %>" placeholder="Enter Your Number" required>
    <br><br>

<input type="submit" value="Update Profile ">
</form>


<% include('../layout/footer.ejs') %>


//controllers
const editProfile = async(req,res)=>{
try {
const id = req.query.id;
const userData = await User.findById({_id:id});

if(userData){
res.render('edit',{user:userData})

}else{
res.redirect('/home');
}

} catch (error) {
console.log(error.message)

}
}

//routes
user_route.get('/edit',auth.isLogin,userController.editProfile);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900