Arrays MongoDB尝试删除数组项时出错
我正在尝试从MongoDB模型中的数组中删除一个项。 这是我的国家模式:Arrays MongoDB尝试删除数组项时出错,arrays,node.js,json,mongodb,post,Arrays,Node.js,Json,Mongodb,Post,我正在尝试从MongoDB模型中的数组中删除一个项。 这是我的国家模式: const CountrySchema = mongoose.Schema({ name: { type: String, required: true }, holidays: [ { day: {type: Number, required: true}, month: {type: Number,
const CountrySchema = mongoose.Schema({
name: { type: String, required: true },
holidays:
[
{
day: {type: Number, required: true},
month: {type: Number, required: true},
description: { type: String, required: true }
},
],
states: { type: [String], required: true }
});
我的数据库中有几个国家的数据,我有一个端点,可以从这个国家删除一个假日。我从请求参数中获得了国家名称,并从邮件中提供了假期信息。就像这样:
{
"day": 10,
"month": 4,
"description": "Random party"
}
所以我的函数是这样的:
function deleteHoliday(req, res) {
var country_name = req.params.country_name;
var n_description = new Country(req.body.description);
let query = Country.update({ 'name': country_name }, { '$pull': { holidays: { description: n_description }}})
query.exec( (err, holidays) =>{
//Check if no errors and send json back
if(err){
res.send(err);
}
res.status(200).json({message:"Holiday removed successfully"});
})
}
我不确定如何执行查询,但在执行查询之前,我遇到了下一个错误:
ObjectParameterError:Document()的参数“obj”必须是一个对象
我只想删除具有给定描述的假日项目。在这种情况下,随机党
谢谢您的帮助。很抱歉我犯了一个错误,我传递的是对象类型国家而不是描述
var n_description = new Country(req.body.description);
should be
var n_description = req.body.description;
阿雷迪解决了 很抱歉,我犯了一个错误,我传递的是对象类型Country而不是描述
var n_description = new Country(req.body.description);
should be
var n_description = req.body.description;
阿雷迪解决了 错误就在这里
var n_description = new Country(req.body.description);
需要一个简单的字符串,请相应地更改它。错误在这里
var n_description = new Country(req.body.description);
需要一个简单字符串,请相应地更改此字符串