How to edit an attribute in the database?

0

I am developing an API where I want to edit information or login . I can already get the user information after logging in 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'});
  }
});

I would like to manipulate this information and save in bank , how do I do this? Like for example , every USER has a atributo called active that is boolean , would like to change it to false when this function inactivate is called, how do I make the USER

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'); 
    
asked by anonymous 29.01.2018 / 19:42

1 answer

1

If I understand your code, after searching the user you can edit the property and save the document again:

User.findOne({
      name: decoded.name
    }, function(err, user) {
        user.active = false;
        user.save(function() {
          res.status(200).send({success: true})
        });
    
29.01.2018 / 20:14