0
I’m developing one API where I want to edit information of users after the login. I already have catch the user information after logging as you can see in the function:
memberinfo
apiRoutes.get('/memberinfo', passport.authenticate('jwt', { session: false}), function(req, res) {
console.log(req.headers);
var token = getToken(req.headers);
console.log(token);
if (token) {
var decoded = jwt.decode(token, config.secret);
User.findOne({
name: decoded.name
}, function(err, user) {
if (err) throw err;
if (!user) {
return res.status(403).send({success: false, msg: 'Falha na autenticação!'});
} else {
res.json({success: true, msg: 'Bem vindo a area dos membros' + user.name + '!', user: user});
}
});
} else {
return res.status(403).send({success: false, msg: 'Nenhuma token foi enviada'});
}
});
Would like to manipulate that information and salvage in the bank, How do I do that?
Like for example, whole USER has a atributo
called active
that is boolean
, would like to change he to false
when that function inactivate is called, how do I make the update of USER at the bank?
apiRoutes.put('/inactivate', passport.authenticate('jwt', { session: false}), function(req, res) {
var token = getToken(req.headers);
if (token) {
var decoded = jwt.decode(token, config.secret);
User.findOne({
name: decoded.name
}, function(err, user) {
if (err) throw err;
if (!user) {
return res.status(403).send({success: false, msg: 'Falha na autenticação!'});
} else {
var UserUpdate = new User({
active: false
});
UserUpdate.update(function(err){
})
}
});
} else {
return res.status(403).send({success: false, msg: 'Nenhuma token foi enviada'});
}
});
USER is a model
of my bank
var User = require('./app/models/user');
Behold that one explanation. You can send the same object and by calling the
save
Mongoose, if the document has a_id
it updates the document in Mongo.– BrTkCa
In this case I have to pass an _id at save time, right?
– Junior Vilas Boas
In that case, yes. Somehow it is necessary to identify the document at the base, there are several ways, such as fetching the document, changing the desired properties and saving later, or sending the entire document to the
save
...– BrTkCa