简体   繁体   中英

Node.js: How to get the incorrect fields in the Error object

I have unique and required fields in my schema below.

An error is returned when any of them are left blank.

How do I find the paths in Error object with information on which fields were left blank?

 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; 

Below is the controller where I save submitted data into the database. Some of the incoming data is empty, so I get an error for the fields that were left blank.

How do I find the incorrect fields in the Error object?

 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 }); }) } }); 

I'm sorry for my bad English.

在尝试失败并从错误消息中提取数据之前,我将检查这些字段是否为空,这可能会因未知错误而失败。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM