猫鼬模式验证不适用于Fawn

时间:2019-01-27 19:23:10

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

我正在开发MEAN应用,并尝试使用FAWN将事务(如行为)实现到MongoDB。我正在使用猫鼬图书馆。当我使用fawn时,数据会跳过模式条件,例如min:1表示SellingPrice。但是,我也在运行中使用{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}});});

1 个答案:

答案 0 :(得分:0)

我在更新中遇到了许多类似的问题,但最终,以下工作成功了:

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