1
我在下面的架構中具有唯一的必填字段。Node.js:如何獲取錯誤對象中的錯誤字段
當它們中的任何一個留空時,將返回錯誤。
如何在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 });
})
}
});
我爲我的英語不好對不起。
可能重複[處理Mongoose驗證錯誤 - 在哪裏以及如何?](https://stackoverflow.com/questions/15012250/handling-mongoose-validation-errors-where-and-how) – Paul