我创建了三个集合,
家庭{familyname,_id}
root {rootname familyId _id}
child {childname rootId familyId _id}
现在,如果我删除家庭,父亲和孩子,也应该删除
到目前为止,我已经尝试过了,但是没有用
router.delete("/:familyId", (req, res, next) => {
const id = req.params.familyId;
Family.remove({ _id: id })
Root.remove({ _id: id })
Child.remove({ _id: id })
.exec()
.then(result => {
res.status(200).json({
message: 'deleted',
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
答案 0 :(得分:0)
您可以按以下方式使用 .remove()
方法返回的承诺:
router.delete("/:familyId", (req, res, next) => {
const id = req.params.familyId;
const family = Family.remove({ _id: id });
const root = Root.remove({ familyId: id });
const child = Child.remove({ familyId: id });
Promise.all([family, root, child]).then(result => {
console.log(result);
res.status(200).json({
message: 'deleted',
});
}).catch(err => {
console.error(err);
res.status(500).json({
error: err
});
});
});