Server side
First we use findOne to find the user.
Then we update the user with an assignment to a new value with an assignment.
We want to to let the database know of this update to the variable user,
so we use the save function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//data to change is stored in x-www-form-urlencoded POST key/value // route to authenticate a user (POST http://localhost:8080/api/:user_email) apiRoutes.put('/:user_email', function(req, res) { console.log('req.params.user_email: ' + req.params.user_email); console.log('req.body.modelKey: ' + req.body.modelKey); console.log('req.body.modelValue: ' + req.body.modelValue); //change the model's value by its key User.findOne({ email: req.params.user_email }, function(err, user) { if (err) throw err; if (!user) { res.json({ success: false, message: 'User not found.' }); } else if (user) { //found valid user, let's change the value console.log('user.name: ' + user.name); console.log('user.email: ' + user.email); console.log('user.password: ' + user.password); console.log('user.admin: ' + user.admin); console.log('user[req.body.modelKey]: ' + user[req.body.modelKey]); user[req.body.modelKey] = req.body.modelValue; user.save(function(err){ if(err) res.send(err); else res.json({message:'updated key' + req.body.modelKey + ' with value ' + req.body.modelValue}); }); } // if/else }); // findOne }); //put |
Client side
PUT http://localhost:8080/api/rtsao@uci.edu
Headers
x-access-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
x-access-email rtsao@uci.edu
Body
x-www-form-urlencoded
modelKey name
modelValue Ricardo