1

I am learning mongodb, but I am running to this wall, and I don't know how to fix it. Please help! It worked before but this time when I make post request I get this error message in Postman "User validation" enter image description here

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;
var 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);

var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var morgan = require('morgan');
var mongoose = require('mongoose');
var port = process.env.PORT || 8080;
var User = require('./app/models/user');


app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


app.use(function(req,res,next){
  res.setHeader('Acess-control-allow-Origin', '*');
  res.setHeader('Acess-control-Allow-Methods', 'GET, POST');
  res.setHeader('Acess-Control-Allow-Headers', 'X-Requested-With, content-type,\Authorization');
  next();
});

app.use(morgan('dev'));

mongoose.connect('mongodb://localhost:27017/myDatabase');

app.get('/', function(req, res){
  res.send('Welcome to thome page');
});

var apiRouter = express.Router();

apiRouter.use(function(req, res, next){
  console.log('Somebody just came to our app');

  next();
});

apiRouter.get('/', function(req, res){
  res.json({message: 'Hooray! Welcome to our api1'});
});

app.use('/api', apiRouter);

apiRouter.route('/users')

         .post(function(req,res){

           var user = new User();

           user.name = req.body.name;
           user.username = req.body.username;
           user.password = req.body.password;

           user.save(function(err){
             if(err){
               if(err.code == 1000)
                 return res.json({success: false, message: "A user with that username already exists"});
               else
                 return res.send(err);
             }
               res.json({message: "User created"});
           });
         });


app.listen(port);
console.log("Magic happens on port " + port);
spaceDog
  • 441
  • 1
  • 7
  • 22
  • This is mongoose, right? You might want to make that a little clearer in your question. – 64_ Jul 05 '16 at 01:04
  • Yea, it is mongoose. – spaceDog Jul 05 '16 at 01:04
  • Possible duplicate of [Express js form data](http://stackoverflow.com/questions/24800511/express-js-form-data). I think you'll find that will clear up your troubles. – Robert Moskal Jul 05 '16 at 01:52
  • Hey, I tried the instructions from the link and is still not working. :( – spaceDog Jul 05 '16 at 02:07
  • Magic happens on port 8080 { name: 'Holly', username: 'hollylawly', 'password ': 'supersecret' } I get this but I am still get same error when I make a post request. – spaceDog Jul 05 '16 at 02:22

2 Answers2

1

I had this problem with postman too. then i decided to use raw data instead of x-www-form-urlencoded with Json(application/json) header. In your case you can use it like this :

{"name" : "Holly", 
"username" : "Hollylawly",
"password" : "supersecret"}

the image for postman req

0

I fixed by reinstalling postman.

Thank you guys for taking your time to answer the question.

Thanks.

spaceDog
  • 441
  • 1
  • 7
  • 22