动态分配数据库文档属性

时间:2017-06-05 03:26:49

标签: javascript mongodb mongoose

我收到此错误:

ERROR: Cast to number failed for value "undefined" at path "floatingPeriodLength"

当我发布到addPricingRule时,根据我添加的规则,我不会传递所有参数。当我没有通过floatingPeriodLength财产时,我收到了错误消息。换句话说,当floatPeriodLength未定义时。有没有办法在不设置某些属性的情况下添加新的定价规则?我还没有按照架构中的要求设置它们......

app.post('/addPricingRule', async function(req, res){
    console.log("/addPricingRule");
    if(!req.user) {
        handleError(res, "User not logged in", "User not logged in", 403);
    } else {
        var newRule = {};
        var aListingID = req.body.aListingID;
        var _id = req.body._id;
        var title = req.body.title;
        var event = req.body.event;
        var amount = req.body.amount;
        var scale = req.body.scale;
        var floatingPeriodStartDay = req.body.floatingPeriodStartDay;
        var floatingPeriodLength = req.body.floatingPeriodLength;
        var orphanPeriodLength = req.body.orphanPeriodLength;
        var specificDatesStartDate = req.body.specificDatesStartDate;
        var specificDatesEndDate = req.body.specificDatesEndDate;
        try {
            var accounts = await Account.find({userID: req.user._id});
            var accountIDs = [];
            accounts.forEach(function(account) {
                accountIDs.push(account._id);
            });
            var listing = await Listing.findOne({
                accountID: {$in: accountIDs},
                aListingID,
            });
            if(_id) {
                await PriceRule.findByIdAndUpdate(_id,
                    {
                        title,
                        event,
                        amount,
                        scale,
                        floatingPeriodStartDay,
                        floatingPeriodLength,
                        orphanPeriodLength,
                        specificDatesStartDate,
                        specificDatesEndDate,
                    }
                );
            } else {
                await PriceRule.create({
                    listingID: listing._id,
                    title,
                    event,
                    amount,
                    scale,
                    floatingPeriodStartDay,
                    floatingPeriodLength,
                    orphanPeriodLength,
                    specificDatesStartDate,
                    specificDatesEndDate,
                });
            }
        } catch(error) {
            handleError(res, error.message, "/addPricingRule", 400);
        }
        res.status(200).json("success");
    }
});

这是架构:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var MessageRule = new Schema({
    listingID: {type: mongoose.Schema.Types.ObjectId, ref: 'listing'},
    message: String,
    title: String,
    event: String,
    days: Number,
    time: Number,
    minNights: {type: Number, default: 1},
    lastMinuteMessage: {type: String, default: ""},
    lastMinuteMessageEnabled: {type: Boolean, default: false},
    reviewEnabled: {type: Boolean, default: false},
    reviewMessage: String,
    sendMessageAfterLeavingReview: Boolean,
});

module.exports = mongoose.model('MessageRule', MessageRule);

1 个答案:

答案 0 :(得分:1)

为什么不做一些像Object.assign一样的副本:

var aListingID = req.body.aListingID;
var _id = req.body._id;
var fields = Object.assign({}, req.body);  // take a copy rather than assign separately 
delete fields._id;                         // discard these properties from the copy
delete fields.aListingID;

然后像{/ p>一样使用fields

 await PriceRule.findByIdAndUpdate(_id, fields);

如果您有其他信息,请指定另一个合并副本

 await PriceRule.create(Object.assign({ listingID: listing._id }, fields))

这样,如果属性不存在,那么你根本就不是为它们创建变量