我有一个列表,其中可以包含一些项目,这些项目可以具有子项目,而这些子项目可以具有其子项目,依此类推。.Iam能够插入新项目及其子项目并更新这些项目的列表更改,但是当尝试插入子项目的子项目时,我的列表没有更新。 这是我插入子项的功能
let addSubItems = (req, res) => {
if (check.isEmpty(req.params.itemId)) {
console.log("error:item id not found")
let apiResponse = response.generate(true, 'no itemId found', 403, null)
res.send(apiResponse)
} else {
ItemModel.findOne({ "itemId": req.params.itemId }).exec((err, result) => {
if (err) {
console.log(err)
let apiResponse = response.generate(true, 'err occured in item find', 403, null)
res.send(apiResponse)
}else if(check.isEmpty(result)){
console.log("no item matched")
let apiResponse = response.generate(true, 'no item found', 403, null)
res.send(apiResponse)
}
else {
console.log("this is the item inside which subitem is to be added")
console.log(result)
let newItem = new ItemModel({
itemId: shortid.generate(),
itemTitle: req.body.itemTitle,
isDone: req.body.isDone
})
newItem.save((err, generatedItem) => {
if (err) {
logger.error(`Error Occured : ${err}`, 'Database', 10)
console.log(err)
let apiResponse = response.generate(true, 'Error Occured.', 500, null)
res.send(apiResponse)
} else {
console.log('Success in new item creation, new generated item is-')
console.log(generatedItem)
result.child.push(newItem)
let listId = req.body.listId
result.save()
console.log("result after saving")
console.log(result)
ListModel.findOneAndUpdate({ "listId": req.body.listId, "items.itemId": req.params.itemId},
{ "$set": { "items.$": result } }, {new:true}).exec((err, updatedlistresult) => {
if (err) {
console.log('error in finding list for save')
console.log(err)
} else if(check.isEmpty(updatedlistresult)){
let apiResponse = response.generate(true, 'updated result is empty', 500, null)
res.send(apiResponse)
}
else {
let apiResponse = response.generate(false, 'item saved', 200, updatedlistresult)
res.send(apiResponse)
}
})
}
})
}
})
}
} 我的模式是-
let listSchema = new Schema({
listId:{
type:String,
default:'',
required: true,
index:true
},
listTitle:{
type:String
},
forUserId:{
type:String,
default: ''
},
items:[ItemSchema]
}) mongoose.model('List',listSchema);
//项目架构
itemSchema.add({
itemId:{
type:String,
default:'',
index:true
},
itemTitle:{
type:String
},
isDone:{
type:Boolean,
default:false
},
child:[itemSchema]
})