How to edit an attribute in the database?

Asked

Viewed 245 times

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.

  • In this case I have to pass an _id at save time, right?

  • 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...

1 answer

1


If I understood your code, after the user search 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})
        });
  • I should use a get, put or post method for such a procedure?

  • Assuming you want to edit the user found in findOne, can be post or put junior

Browser other questions tagged

You are not signed in. Login or sign up in order to post.