I am trying to send a PUT request to the API but I get an error. I tried casting in DAO file but wasn’t helpful. I tried the other solutions to this issue but nothing could help me.
json data
{
"citizen_id": "62595e0a2bcb1344ed6ab683",
"user_id": "1320",
"name": "cctyv tesey",
"email": "[email protected]",
"password": "abc123",
"nic": "921242340V",
"qualification": "a level",
"username": "tesy123",
"address": "4/3, isurupura road, kothalwala, kaduwela",
"profession": "software developer"}
citizenDAO.js
static async updateCitizens(userId, citizenId, date) {
try {
const updateCitizen = await citizens.updateOne(
{ user_id:userId,_id: ObjectId(citizenId)
},
{ $set: { date: date } },
)
return updateCitizen
} catch (e) {
console.error(`Unable to update the citizen: ${e}`)
return { error: e }
}
}
citizen.controller.js
static async apiUpdateCitizens(req, res, next) {
try {
const citizenId = req.body.citizen_id
//const text = req.body.text
const date = new Date()
const citizenResponse = await CitizensDAO.updateCitizens(
citizenId,
req.body.user_id,
date,
)
var { error } = citizenResponse
if (error) {
console.error(`Unable to update the citizen: ${error}`)
}
if (citizenResponse.modifiedCount === 0) {
throw new Error(
"unable to update citizen - user may not be same",
)
}
res.json({ status: "success" })
} catch (e) {
res.status(500).json({ error: e.message })
}
}