gpt4 book ai didi

javascript - 动态分配数据库文档属性

转载 作者:行者123 更新时间:2023-12-03 04:10:19 27 4
gpt4 key购买 nike

我收到此错误:

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

最佳答案

为什么不做一些像 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;

然后使用字段,例如

 await PriceRule.findByIdAndUpdate(_id, fields);

当您有其他信息时,分配另一个合并副本

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

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

关于javascript - 动态分配数据库文档属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44361267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com