【问题标题】:Mongoose schema validation is not working with FawnMongoose 模式验证不适用于 Fawn
【发布时间】:2019-06-20 20:46:17
【问题描述】:

我正在开发一个 MEAN 应用程序,并尝试使用 FAWN 对 MongoDB 实现类似行为的事务。我正在使用猫鼬库。当我使用小鹿时,数据有点跳过模式条件,例如 min: 1 for sellPrice。但是,我也在运行中使用 { useMongoose: true }。

    var mongoose = require('mongoose');
    var Fawn = require('fawn');    
    Fawn.init(mongoose);

    //Function code is as follow

        var task = Fawn.Task();
        let item = await Item.findById(req.params.id);

        task.update('items', {_id: item._id}, {type: req.body.type, name: req.body.name, sku: req.body.sku, openingStock: req.body.openingStock, availableStock: req.body.availableStock, purchasePrice: req.body.purchasePrice, sellingPrice: req.body.sellingPrice, profitMargin: req.body.profitMargin, reorderLevel: req.body.reorderLevel, preferredVendor: req.body.preferredVendor});
        task.update('items', {_id: item._id}, {sellingPrice: -50});//This line should fail because min value for sellingPrice is 1
        task.run({ useMongoose: true })
            .then(function(){res.json(item);})
            .catch(function(error){res.json({error: {"message": error}});});

【问题讨论】:

    标签: node.js mongodb mongoose mean-stack mongoose-schema


    【解决方案1】:

    我在更新时遇到了许多类似的问题,但最后,以下方法奏效了:

    try {
      await new Fawn.Task()
      .update('collection', 
                   { _id: mongoose.Types.ObjectId(req.params.id) } , 
                   { name: 'test', desc: 'description })
      .run({useMongoose: true});
    } 
    catch(err) { console.log(err); }
    

    【讨论】:

    • 如何解决这个警告:(node:57487) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
    猜你喜欢
    • 2018-03-03
    • 2021-12-01
    • 2021-12-18
    • 2018-10-06
    • 2013-11-13
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多