2

I have a account with mongolab(mlab). I am trying to post a data for the users using postman add-on from chrome browser. I am getting error always. I could not able to post a data. I have tried with other diffrent ways. but no luck.

any one help me to sort this issue?

here is my api.js :

var
    User        = require('../models/user'),
    config      = require('../../config'),
    secretKey   = config.secretKey; 


module.exports = function( app, express ) {

    var api = express.Router();

    api.post('/signup', function (req, res) {

        var user = new User({

            name:req.body.name,
            username:req.body.username,
            password:req.body.password

        });

        user.save(function(err){

            if(err){
                res.send(err);
                return;
            }

            res.json({message:'User has been Created!'});
        });
    });


    api.get('/users', function(req, res) {

        User.find({}, function( req, users){
            if(err) {
                res.send(err);
                return;
            }

            res.json(users);
        })

    });

    return api;

}

config.js :

module.exports = {
    "database":"mongodb://xxx:xxxx@ds015700.mlab.com:15700/arifstory",
    "port" : process.env.PORT || 3000,
    "secretKey" : "YourSecretKey"
}

And the user.js :

var
    mongoose    = require('mongoose'),
    Schema      = mongoose.Schema,
    bcrypt      = require('bcrypt-nodejs');


var UserSchema = new Schema({

    name : String,
    userName:{ type:String, required:true, index : { unique: true }},
    password : { type:String, required : true, select : false }

});

UserSchema.pre('save', function(next) {

    var user = this;

    if(!user.isModified('password')) return next();

    bcrypt.hash( user.password, null, null, function(err, hash) {
        if(err) return next(err);

        user.password = hash;
        next();
    });

});

UserSchema.methods.comparePassword = function( password ) {

    var user = this;

    return bcrypt.compareSync(password, user.password);

}

module.exports = mongoose.model('User', UserSchema);

I really unable to understand the issue here. please any one help me?

error

{
  "message": "User validation failed",
  "name": "ValidationError",
  "errors": {
    "userName": {
      "message": "Path `userName` is required.",
      "name": "ValidatorError",
      "properties": {
        "type": "required",
        "message": "Path `{PATH}` is required.",
        "path": "userName"
      },
      "kind": "required",
      "path": "userName"
    },
    "password": {
      "message": "Path `password` is required.",
      "name": "ValidatorError",
      "properties": {
        "type": "required",
        "message": "Path `{PATH}` is required.",
        "path": "password"
      },
      "kind": "required",
      "path": "password"
    }
  }
}
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • Is their any specific message with the error, I have never used chrome extension to add data so this may sound stupid. – Gandalf the White Apr 04 '16 at 06:29
  • @GandalftheWhite - please see my update for `error` what i receive – 3gwebtrain Apr 04 '16 at 06:33
  • @3gwebtrain in your schema you have `userName` but you are setting `username` in api.js – war1oc Apr 04 '16 at 08:54
  • @war1oc - I have made the correct from your suggestion. still getting issue. ( same error ) – 3gwebtrain Apr 04 '16 at 09:02
  • can you show what you are posting from postman (the form data)? and can you also show `console.log(req.body)` in the api.post signup method? – war1oc Apr 04 '16 at 09:06
  • @war1oc - after I clearing the cache of the browser and restarting, works well. very thanks to you. please add your comment as answer to accept – 3gwebtrain Apr 04 '16 at 09:10

1 Answers1

0

The data sent from chrome could be undefined.

Print it in console by including the below 2 lines above user.save function in api.js file: console.log(req.body.username); console.log(req.body.password);

if it shows as undefined, then make sure to check "x-www-form-urlencoded" under "body" in Postman and provide username and password under that.