2017-08-08 7 views
1

以下のスキーマには、一意の必須フィールドがあります。Node.js:Errorオブジェクトの不正なフィールドを取得する方法

空白のままにするとエラーが返されます。

Errorのオブジェクトのパスはどのフィールドを空白にしておくのですか?以下は

var mongoose = require('mongoose'); 
 
var uniqueValidator = require('mongoose-unique-validator'); 
 
    
 
var Schema = mongoose.Schema; 
 

 
var kullaniciShema = new Schema({ 
 
    gsm: String, 
 
    email: String, 
 
    kullaniciAdi: { type: String, required: true, unique: true }, 
 
    sifre: { type: String, required: true}, 
 
}, { collection: 'kullanici' }); 
 

 
kullaniciShema.plugin(uniqueValidator); 
 

 
var Kullanici = mongoose.model('Kullanici', kullaniciShema); 
 

 
module.exports = Kullanici;

私はデータベースにデータを提出し、セーブコントローラです。入ってくるデータの中には空のものがありますので、空欄になっているフィールドにはエラーが発生します。

Errorオブジェクトで間違ったフィールドを検索するにはどうすればよいですか?

var yeniKullanici = new Kullanici({ 
 
    gsm: req.body.gsm, 
 
    email: req.body.email, 
 
    kullaniciAdi: req.body.kullaniciAdi, 
 
    sifre: req.body.sifre, 
 
}); 
 

 
yeniKullanici.save(function(err){ 
 
    if (err) { 
 
     //var error=new err("hata"); 
 
     //console.log(error.path); 
 
     selectAllFromKullanici((result) => { 
 
      //console.log(forUsersError.length); 
 
      forUsersError[forUsersError.length] = assert.equal(error.errors['ad'].message, 'Path `name` is required.'); 
 
      res.render("kullanicilar", { kullanicilar: result, hata: forUsersError }); 
 
     }) 
 
    } else { 
 
     selectAllFromKullanici((result) => { 
 
      res.render("kullanicilar", { kullanicilar: result }); 
 
     }) 
 
    } 
 
});

私は私の悪い英語のためにごめんなさい。

+1

[Mongoose検証エラーの処理 - どこに?](https://stackoverflow.com/questions/15012250/handling-mongoose-validation-errors-where-and-how)の可能な複製 – Paul

答えて

0

エラーが発生し、不明なエラーで失敗する可能性のあるエラーメッセージからデータを抽出する前に、フィールドが空であるかどうかを確認します。

関連する問題