Thursday 29 December 2016

res.json not working when making rest API calls.

Here i have this set up but i dont get what i expect it to show when calling onto the API.I expect this: { "success" : true, "message" : "Successful created new user" } but i get this: { "__v": 0, "username": "test", "password": "$2a$10$3khe9TBep0aECUaKxayPl.0NsUMguxhuHOnzqNYWWq0JDkN0Fk0bG", "_id": "5864d9afd6140cb7a6b6d4c9" } app.post('/user', function(req, res) { if (!req.body.username || !req.body.password) { res.json({success: false, msg: 'Please pass name and password.'}); } else { var newUser = new User({ username: req.body.username, password: req.body.password }); // save the user newUser.save(function(err) { if (err) { return res.json({success: false, msg: 'Username already exists.'}); } res.json({success: true, msg: 'Successful created new user.'}); }); } });

Submitted December 29, 2016 at 09:09AM by farhansyed7911

No comments:

Post a Comment