Skip to content Skip to sidebar Skip to footer

Res.redirect Showing Old Information After Form Submission?

I have a node.js app in which I created a user profile page. I made it editable so they can update their name if they want to. However, when I click the 'Save Changes' button on t

Solution 1:

You can directly use findOneAndUpdate() function to update user profile. Try as below:

const express = require('express');
const router = express.Router();
const { ensureAuthenticated } = require('../config/auth');
const mongoose = require('mongoose');
const User = require('../models/User');

router.post('/', ensureAuthenticated, (req, res) => {
  User.findOneAndUpdate({_id:req.user.id},{name: req.body.name},{new: true},(err,doc)=>{
       console.log('#### Updated Record ####',doc);
       res.redirect('/profile');
  });   
});



module.exports = router;

Post a Comment for "Res.redirect Showing Old Information After Form Submission?"