大家好我刚刚尝试创建更新功能并使用Postman我想更新firstName
字段。控制台中的结果是HTTP / 1.1" 200但没有更新。
这是邮递员身上的结果
{
"user_id": "5adaa55c0364d01cd9478492",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1YWRhYTU1YzAzNjRkMDFjZDk0Nzg0OTIiLCJpYX4OTJ9.JpDShf4iBFBCQ5rGflKbsj3jisl-PS68UOTnVpxNyXs",
"data": {
"userData": [
{
"address": [
{}
]
}
]
}
}
这是我尝试更新的内容
{
"data": {
"userData": [
{
"firstName": "George"
}
]
}
}
以下是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
}],
}],
}
});
}
});
}
这是我route.js
router.route('/users/:user_id/data')
.get(requireAuth, AuthenticationController.index)
.put(requireAuth, AuthenticationController.update);
答案 0 :(得分:0)
你没有打电话给你的猫鼬模特。您正在调用由req
中间件放置到requireAuth
的用户对象来解码JWT。
您需要调用/使用您实际的mongoose User
模型:
const User = require('./path/to/mongoose/model/User')
...
// assuming the decoded JWT user object has an `id` property
User.findByIdAndUpdate(req.user.id, req.body)
.exec()
.then(doc => res.json(doc)
.catch(err => res.json(err))
http://mongoosejs.com/docs/api.html#findbyidandupdate_findByIdAndUpdate