Mongoose模式验证不适用于Fawn

问题描述 投票:0回答:1

我正在开发一个MEAN应用程序并尝试使用FAWN实现MongoDB等行为之类的事务。我正在使用猫鼬图书馆。当我使用fawn时,数据有点像跳过模式条件,如min:1 for 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}});});
node.js mongodb mongoose mean-stack mongoose-schema
1个回答
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); }
© www.soinside.com 2019 - 2024. All rights reserved.