express - Why is my update user model route not working (mongodb)? -


update: posted few days ago , haven't managed answer. able help? cannot work out why i'm getting error: 'cannot set property 'username' of null. seems code i'm running capture current user id isn't working cannot see i'm going wrong here.

i can't work out why update route mongodb user model not working.

so idea user presented form current user details preloaded , can edit form , should update user schema.

here's edit profile form:

<div class="row"> <div id="user-image-preview" class="col-md-4 col-sm-12 user-image-preview"> </div> <div class ="reg-container-form col-md-8 col-sm-12"> <div class="jumbotron form"><h2><i class="fa fa-user-plus" aria-hidden="true"></i> update profile</h2></div> <form id="register" action = "/register" method="post"> <div class="form-group has-feedback">     <i class="fa fa-user" aria-hidden="true"></i>     <label for="username">username</label>         <input id="username" type = "text" class = "form-control" value = "<%=currentuser.username%>" name="username">  </div>  <div class="form-group has-feedback">     <i class="fa fa-envelope" aria-hidden="true"></i>     <label for="email">email</label>         <input id="email" type = "email" class = "form-control" value = "<%=currentuser.email%>" name="email">  </div>  <div class="form-group">     <div><i class="fa fa-picture-o" aria-hidden="true"></i>  <label for="img">image</label></div>         <input id="user-image-set" type = "text" class ="form-control" placeholder = "enter image url" name = "image">           <a href="#"><div id="eye-button"><i id="preview-icon" class="fa fa-eye" aria-hidden="true"></i></div></a> </div>      <button id="submit-login" type ="submit" class="btn btn-primary btn-lg disabled">confirm changes</button> </form> </div> </div> 

form accessed through following route:

router.get("/profile", function(req, res){ res.render("profile") }) 

user schema is:

var userschema = new mongoose.schema({ username: string, password: string, image: string, email: string }); 

so essentially, want user re-enter params in input fields, click on confirm , redirected /blogs schema having been updated.

i started write route honest got lost on first line. i've created update routes dynamically generated elements (comments, blogs) seems different passing through id etc in url. not sure start here...

i've tried using finduserbyid method follows:

router.post("/update-user", function(req, res, next){ user.findbyidandupdate(req.body.user_id, req.body.user, function  (err,updateduser){     if(err){         res.redirect("/profile")         console.log(err);     } else {         res.redirect("/blogs");     } }) }) 

this code runs fine (as in redirects /blogs) doesn't updae model. i've tried inserting updateduser.save():

router.post("/update-user", function(req, res, next){ user.findbyidandupdate(req.body.user_id, req.body.user, function(err,updateduser){     if(err){         res.redirect("/profile")         console.log(err);     } else {         updateduser.save();         res.redirect("/blogs");     } }) }) 

this produces following error:

typeerror: cannot read property 'save' of null 

i tried changing code find current userid follows (following advice received below):

router.post('/update-user',function (req,res,next){ user.findone({_id: req.body._id},function (err,user){ if(err){ res.redirect("/profile") } else { user.username = req.body.username; user.email = req.body.email; user.image = req.body.image; user.save() }} )}); 

this produces following error:

typeerror: cannot set property 'username' of null 

very stuck what's going wrong here!

what need first find user id pass in new values.

updated:

    router.post('/update-user',function (req,res,next){  user.findone({_id: req.body._id},function (err,user){ if(err){     res.redirect("/profile") } else { user.username = req.body.username; user.email = req.body.email; user.image = req.body.image; user.save() }} )}); 

i put in simple terms, i'm sure can take care of error handling etc.

good luck


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -