Exceptions Exceptions - 8 months ago 26
Node.js Question

Mongoose not saving data with no error

I have this code

User.findOne({ email: req.body.email }, function(err, user) {
if (user) {
return res.status(400).json([{ msg: 'The email address you have entered is already associated with another account.' }]);
user = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password,
mobile: req.body.mobile
user.save(function(err) {
res.json({ token: generateToken(user), user: user });

I tried sending a request with postman and I did get a response back with the user object which means there is no err in the save function

"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJleGNlcHRpb25zLnNnIiwic3ViIjoiNTc5NWQ0NzQxYmEzZWQwODE1ZTgzY2NmIiwiaWF0IjoxNDY5NDM3MDQ0LCJleHAiOjE0NzAwNDE4NDR9.2otkcPkJgsXvR8QOHAojDJ5YCxR7Uc2E4ApS77T55F8",
"user": {
"__v": 0,
"created_at": "2016-07-25T08:57:24.612Z",
"updated_at": "2016-07-25T08:57:24.612Z",
"name": "Test",
"email": "Test@hotmail.com",
"password": "$2a$10$3UmABiDPeo6iHZ.DFbwOO.1ANpUWQmwr86bYbTmRuFedsbDcE0bbC",
"mobile": 12345,
"_id": "5795d4741ba3ed0815e83ccf"

However there is no entry of this inside my DB. I've check through my database with Robomongo and I'm sure that there is no data. What did I missed out?


Your arent handling error while saving the collection, replace your user.save function with


      res.json({ token: generateToken(user), user: user });

By this you will be able to track whether the data is saved or not and what can be the associated error.