我有兩個模型連接和客戶。在 Connection 中參考了客戶。我想從客戶檔案中洗掉客戶并從連接中洗掉或更新客戶參考
客戶模型是
const mongoose = require('mongoose')
const Customer = mongoose.model('Customer', {
cusName: {
type: String,
required: true,
trim: true
},
nic: {
type: String,
default: false
},
cNumber: {
type: String,
default: true
},
cBranch: {
type: String,
default: true
},
cAddress: {
type: String,
default: true
},
cEmail: {
type: String,
default: true
},
})
module.exports = Customer
和連接模型是
const mongoose = require('mongoose')
const Customer = mongoose.model('Customer', {
cusName: {
type: String,
required: true,
trim: true
},
nic: {
type: String,
default: false
},
cNumber: {
type: String,
default: true
},
cBranch: {
type: String,
default: true
},
cAddress: {
type: String,
default: true
},
cEmail: {
type: String,
default: true
},
})
module.exports = Customer
連接再次被支付模型參考,當前執行的洗掉操作如下。
router.delete('/customer/:id', async (req, res) => {
try {
const customer = await Customer.findByIdAndDelete(req.params.id)
if (!customer) {
return res.status(404).send()
}
res.send(customer)
} catch (e) {
res.status(500).send()
}
})
uj5u.com熱心網友回復:
首先在連接中添加一個客戶 ID 欄位
const mongoose = require('mongoose')
const Customer = mongoose.model('Customer', {
...
cusId: {
type: String,
required: true,
},
...
})
module.exports = Customer
其次,洗掉和具有相同 cusId 的客戶
router.delete('/customer/:id', async (req, res) => {
try {
const customer = await Customer.findByIdAndDelete(req.params.id)
const connection = await Connection.deleteMany({ cusId: req.params.id })
if (!customer) {
return res.status(404).send()
}
res.send({ message: "User has been deleted" })
} catch (e) {
res.status(500).send()
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368436.html
