0

I'm implementing a simple local auth with express and MongoDB (using Mongoose) and for some reason the server sends back an empty response object for user, but the token gets sent in the response. I included a console.log statement immediately before the return to try and debug it a little bit, and the object logged there is the correct one with all of the data. This is what the create code looks like:

import mongoose from 'mongoose';
import jwt from 'jsonwebtoken';
import json from '../helpers/json';

var User = mongoose.model('User');

module.exports = function() {
  var obj = {};
  
  obj.create = function (req, res) {
     var roles = ['authenticated'];

     User.count({}, (err, len) => {
        if (!len) {
           roles.push('admin');
        }

        var user = new User(req.body);
        user.roles = roles;
        user.provider = 'local';
        user.token = jwt.sign(user, global.config.secret, {expiresIn: 10800});
        user.save((err, user) => {
            if (err) {
               return json.bad(err, res);
            }

            json.good({
                record: user,
                token: user.token
            }, res);
       });
     });
    };
  
  return obj;
 };

Like I said, I had included a console.log statement and the user will display properly.

If you are wondering, the json.good is a helper function that I wrote that basically looks like this

module.exports = {
  good: function (obj, res) {
     res.send({
       success: 1,
       res: obj
     });
  },

  bad: function (err, res) {
     var obj = {
        success: 0,
        res: err
     };

     if (obj.res.errors) {
       obj.res.messages = [];

       for (var i in obj.res.errors) {
          obj.res.messages.push(obj.res.errors[i].message;
       }
    
       obj.res.messages = obj.res.messages[0];

     }

     res.send(obj);

     }
};

I am also allowing the correct headers and methods in my express file. This code is identical to code that I have used before, but I am missing something it seems.

Update

I figured out the problem, it was in my model. I had

UserSchema.methods = {
     toJSON: function() {
       var obj = this.toObject();
       delete obj.password;
       delete obj.following;
    }
};

I had forgotten to return the obj at the end. Thanks everyone!

halfer
  • 19,824
  • 17
  • 99
  • 186
bashkir
  • 11
  • 4

1 Answers1

0

Make sure that the Value Type in MongoDB matches the Variable type... So if you have a Key named 'fav' that is an Int32, Then make sure that the variable you use to find it is an Int32.

yungtechboy1
  • 36
  • 1
  • 3
  • While I understand what you are saying, I am failing to see how it applies here. The new user object that I create saves the type String values correctly (username, email, password, token blah blah) that persists correctly in the database. When I console.log this object before it is sent I get back the correct user Object as it appears in the database; however, when I send the object to the client, it is sent as an empty object. I might just be failing to understand the implications of what you said, so if you feel please explain more and hopefully I can resolve this – bashkir Jul 29 '16 at 04:27
  • Nevermind! It had been resolved! Thank you – bashkir Jul 29 '16 at 04:48