【发布时间】:2018-01-16 15:34:34
【问题描述】:
我在下面的架构中有唯一的必填字段。
当其中任何一个为空时返回错误。
如何在 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 });
})
}
});
对不起,我的英语不好。
【问题讨论】:
标签: javascript node.js mongoose