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当您承认邮件的“正文”中没有发送密钥时,code>将未定义。因此,不要将所有键都分配给变量,也不要被未定义的键绊倒,只需使用请求主体的副本,修改为您将主要使用的键即可。优雅的代码删除错误。
await PriceRule.create(Object.assign({ listingID: listing._id }, fields))