Reputation: 125
I am extremely new to javascript and found Brad Traversy's video Node.js with Passport Authentication and followed his video to a t.
It worked for me, but then I wanted to add more. I created a custom dashboard and navigation. One page I added was a user profile page. I added this because I want to be able to update the user's information if possible.
Now I'm stuck. I think I'm having an issue with my .findOneAndUpdate function that I placed in my route index.js. I may not be using the funcion correctly. I am currently console logging req.body.name as well as req.user.name.
req.body.name is what the user is inputting as the new name, req.user.name is the original name.
top and bottom of index.js
const express = require('express');
const router = express.Router();
const { ensureAuthenticated } = require('../config/auth');
const mongoose = require('mongoose');
require('../models/User');
const User = mongoose.model('User');
router.post('/', (req, res) => {
updateRecord(req, res);
});
function updateRecord(req, res) {
User.findOneAndUpdate({ "_id": req.body._id },{
$set: {
"name": req.body.name
}
}, { new: true }, (err, doc) => {
if (!err) {
console.log(req.user.name);
console.log(req.body.name);
res.redirect('/profile');
}
else {
console.log('Error during record update : ' + err);
}
});
}
module.exports = router;
edit.ejs
<form action="./profile" method="POST">
<div class="form-group">
<label for="">Full Name</label>
<input type="text" class="form-control" value="<%= name %>"
name="name">
</div>
<div class="form-group" >
<label for="">email</label>
<input type="text" class="form-control" value="<%= email %>" readonly>
</div>
<div class="form-row">
<div class="form-group">
<button type="submit" class="btn btn-info"><a href="profileEditor">
</a> Save Changes</button>
</div>
<p> </p>
<div class="form-group">
<button class="btn btn-dismiss"><a href="./profile">Exit</a>
</button></a>
</div>
</div>
</form>
User.js
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
EDIT
I followed THEWOLF's instructions and it is now updating to the database! However, now when I click my 'Save Changes' button, it redirects to the profile page but displays the old name. Only when you refresh the page or navigate away from it does the updated name show on my website.
Here is new index.js code
router.post('/', (req, res) => {
updateRecord(req,res);
res.redirect('/profile');
});
function updateRecord(req, res) {
User.findOne({_id:req.user.id},(err,doc)=>{
//this will give you the document what you want to update.. then
doc.name = req.body.name;
doc.save(function(err,doc){
});
});
Upvotes: 2
Views: 8222
Reputation: 557
await UserSchema.findByIdAndUpdate({ _id: _id }, { name:req.body.name }, (err, updated) => {
if (!err) {
return res.status(201).send({
status: true,
message: "User Account Updated Successfully!",
});
}
else{
return res.status(500).send({
status: false,
error: err.message,
});
}
}).clone();
Upvotes: 0
Reputation: 1155
in your requiring User model do it like this
const User=require('../models/User');
then
function updateRecord(req, res) {
User.findOne({_id:req.body.id},(err,doc)=>{
//this will give you the document what you want to update.. then
doc.name = req.body.name; //so on and so forth
// then save that document
doc.save(callback);
});
}
Upvotes: 3