Hello everyone, I recently attempted to implement my update function and tested it using Postman. I wanted to update the firstName
field, but despite receiving a "HTTP/1.1" 200 response in the console, nothing was actually updated.
This is the response body displayed in Postman:
{
"user_id": "5adaa55c0364d01cd9478492",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1YWRhYTU1YzAzNjRkMDFjZDk0Nzg0OTIiLCJpYX4OTJ9.JpDShf4iBFBCQ5rGflKbsj3jisl-PS68UOTnVpxNyXs",
"data": {
"userData": [
{
"address": [
{}
]
}
]
}
}
This is the content that I attempted to update:
{
"data": {
"userData": [
{
"firstName": "George"
}
]
}
}
Below is the code snippet for my update function located in authentication_controller.js
:
exports.update = function (req, res, next) {
var email = req.body.email;
var password = req.body.password;
var role = req.body.role;
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var phone = req.body.phone;
var number = req.body.number;
var street = req.body.street;
var city = req.body.city;
var postcode = req.body.postcode;
var user = req.user;
user.save(function (err) {
if (err) {
return next(err)
} else {
res.json({
user_id: user._id,
token: tokenForUser(user),
data: {
email: email,
password: password,
userData: [{
role: role,
firstName: firstName,
lastName: lastName,
phone: phone,
address: [{
number: number,
street: street,
city: city,
postcode: postcode
}],
}],
}
});
}
});
}
Furthermore, here is the excerpt from my route.js
file:
router.route('/users/:user_id/data')
.get(requireAuth, AuthenticationController.index)
.put(requireAuth, AuthenticationController.update);