Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态分配数据库文档属性_Javascript_Mongodb_Mongoose - Fatal编程技术网

Javascript 动态分配数据库文档属性

Javascript 动态分配数据库文档属性,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我得到了这个错误: ERROR: Cast to number failed for value "undefined" at path "floatingPeriodLength" 当我发布到addPricingRule时,我不会根据我添加的规则传递所有参数。当我没有传递floatingPeriodLength属性时,我得到了错误。换句话说,当floatingPeriodLength未定义时。有没有办法在不设置某些属性的情况下添加新的定价规则?我没有按照架构中的要求设置它们 app.post

我得到了这个错误:

ERROR: Cast to number failed for value "undefined" at path "floatingPeriodLength"
当我发布到
addPricingRule
时,我不会根据我添加的规则传递所有参数。当我没有传递
floatingPeriodLength
属性时,我得到了错误。换句话说,当floatingPeriodLength未定义时。有没有办法在不设置某些属性的情况下添加新的定价规则?我没有按照架构中的要求设置它们

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

为什么不做一些类似的事情,比如用:

然后像以后一样使用
字段

 await PriceRule.findByIdAndUpdate(_id, fields);
当您有其他信息时,请分配另一个合并副本

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

这样,如果属性不存在,那么您就不需要为它们创建变量了

谢谢您的回答,但这会修复我得到的错误吗?我认为这是一种更优雅的编码方式(我将使用它),但我不确定它是否解决了我的问题,是吗?谢谢您的帮助。@TomKrones您收到错误是因为
var floatingPeriodLength=req.body.floatingPeriodLength
未定义。因此,不要将所有键都分配给变量,也不要被未定义的键绊倒,只需使用请求主体的副本,修改为您将主要使用的键即可。优雅的代码删除错误。
 await PriceRule.create(Object.assign({ listingID: listing._id }, fields))