gpt4 book ai didi

javascript - Node.js:如何获取 Error 对象中不正确的字段

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:15 26 4
gpt4 key购买 nike

我的架构中有独特且必填的字段。

其中任何一个留空都会返回错误。

如何查找 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;

下面是我将提交的数据保存到数据库中的 Controller 。一些传入数据为空,因此我收到留空字段的错误。

如何找到 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:如何获取 Error 对象中不正确的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com