【问题标题】:mongoose schema validation failed with findOneAndReplace猫鼬模式验证因 findOneAndReplace 失败
【发布时间】:2022-01-09 13:26:14
【问题描述】:

当我使用猫鼬“findOneAndReplace”更新数据时遇到问题, 我收到验证架构错误, 特别是这些字段似乎是空的。

使用相同的数据,我对“创建”和“删除”等其他 crud 操作没有问题, 所以绝对与架构或数据无关

无论如何这里是我的代码:

Angular 中的服务

    updateCustomer(customer){
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.put(this.UpdateCustomer+'/'+customer._id, JSON.stringify(customer), {headers: headers})
    .map((response: Response) => response.json())
}

猫鼬放

app.put('/api/aggiorna_cliente/:id', function(req, res, next) {
console.log(JSON.stringify(req.body)) --> IS POPULATED
Clienti.findOneAndReplace(
    {_id:req.params.id},
    {$set:{
        address:req.body.address,
        brand:req.body.brand,
        cap:req.body.cap,
        city:req.body.city,
        civico:req.body.civico,
        email:req.body.email,
        fiscalcode:req.body.fiscalcode,
        provincia:req.body.provincia,
        utente:req.body.utente
        }
    }, 
    function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
});

客户端架构

            const mongoose = require('mongoose');

        const clientiSchema = mongoose.Schema({
            utente:{
                type: String,
                required:true
            },
            cap:{
                type: Number,
                required:true
            },
            civico:{
                type: String,
                required:true
            },
            city:{
                type: String,
                required:true
            },  
            address:{
                type: String,
                required:true
            },
            fiscalcode:{
                type:String,
                required:true
            },
            email:{
                type:String,
                required:true
            },      
            brand:{
                type:String,
                required:true
            },      
            provincia:{
                province: String,
                sigle: String
            }
        });

        const Clienti = mongoose.model('Clienti',clientiSchema);
        module.exports = Clienti;

会是什么?

【问题讨论】:

    标签: node.js mongoose mongoose-schema


    【解决方案1】:

    不要将 $set 与 Mongoose 一起使用(这是本地 Mongo 运算符)。第二个参数必须是包含要更新的字段的对象 (doc)。不要添加$set。 Mongoose 认为$set 是更新的关键,所以它没有通过你的验证。

    Clienti.findOneAndReplace(
        {_id:req.params.id},
        {
            address:req.body.address,
            brand:req.body.brand,
            cap:req.body.cap,
            city:req.body.city,
            civico:req.body.civico,
            email:req.body.email,
            fiscalcode:req.body.fiscalcode,
            provincia:req.body.provincia,
            utente:req.body.utente
        },
    

    【讨论】:

    • 好建议,谢谢,但不幸的是似乎不起作用
    • 嗯,这就是文档所说的......定义“它不起作用”?
    • 好吧,这似乎是一个验证问题,这里来自控制台的消息:ValidationError: Clienti validation failed: brand: Path brand is required., email: Path email is required.,财政代码:路径fiscalcode 是必需的。地址:路径address 是必需的。城市:路径city 是必需的。公民:路径civico 是必需的。上限:路径cap 是必需的。, utente:路径utente 是必需的。但实际上这些字段不是空的
    • 如果你console.log()你的更新对象呢?是否正确记录?每个键和每个值?
    • 嗯,是的,我在 findOneAndReplace 之前放了一个 console.log 我得到的:{"provincia":{"province":"Aosta","sigle":"AO"},"_id ":"5ede0d15eee9e6380cfd4cbf","brand":"test","email":"test@test","fiscalcode":"01555510260","address":"via guaiane 95","city":"noventa di piave ","civico":"32","cap":30020,"utente":"5ede0ab57dd94706f0b0f30b","__v":0}
    【解决方案2】:

    我遇到了类似的问题,并通过将 null 作为第三个参数传递来解决它,所以对于您的问题,它将是

    Clienti.findOneAndReplace(
        {_id:req.params.id},
        {
            address:req.body.address,
            brand:req.body.brand,
            cap:req.body.cap,
            city:req.body.city,
            civico:req.body.civico,
            email:req.body.email,
            fiscalcode:req.body.fiscalcode,
            provincia:req.body.provincia,
            utente:req.body.utente
        }, 
        null,
        function (err, post) {
            if (err) return next(err);
            res.json(post);
        });
    });

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2011-11-14
      • 2021-06-20
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多