2016-04-04 28 views
3

私はmongolab(mlab)のアカウントを持っています。私はpostmanアドオンをクロームブラウザから使用して、ユーザーのデータを投稿しようとしています。私はいつもエラーが出ています。私はデータを投稿できませんでした。私は他の方法で試しました。運がない。`mongolab` - 投稿時にエラーが発生し、データを投稿できません

この問題を解決するのに手伝ってください。ここ

は私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:[email protected]:15700/arifstory", 
    "port" : process.env.PORT || 3000, 
    "secretKey" : "YourSecretKey" 
} 

そして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); 

ここでの問題を理解するために、私は本当にできません。誰か助けてください私を助けてください?

エラー

{ 
    "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" 
    } 
    } 
} 
+0

これらのメッセージにエラーがあります。データを追加するためにchrome拡張子を使用したことがないので、これは愚かに聞こえるかもしれません。 –

+0

@GandalftheWhite - 私のアップデートは 'error'のものをご覧ください – 3gwebtrain

+0

@ 3gwebtrainあなたのスキーマには' userName'がありますが、api.jsに 'username'を設定しています – war1oc

答えて

0

クロムから送信されるデータは未定義である可能性があります。

api.jsファイルのuser.save関数の上に次の2行を含めてコンソールに表示します。 console.log(req.body.username); console.log(req.body.password);

未定義の場合は、Postmanの "body"の下にある "x-www-form-urlencoded"にチェックを入れ、その下にユーザー名とパスワードを入力してください。

関連する問題