Reputation: 780
I am trying to remove logged-in users objectId from an array field called interest
but it doesnt work.
model
const projectSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
isShowcase: {
type: Boolean,
required: false,
default: false
},
isCollab: {
type: Boolean,
required: false,
default: false
},
title: {
type: String,
required: true,
trim: true
},
description: {
type: String,
required: true,
trim: true
},
thumbnail: {
type: String,
required: true,
trim: true
},
media: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Media',
required: false,
}],
skillsNeeded: [{
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Skill'
}],
contributors: [{
user: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'User'
},
role: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Skill'
},
required: false,
}],
isOpen: {
type: Boolean,
required: false,
default: false
},
interest: [{
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'User'
}]
}, {
timestamps: true
})
the route to remove interest
// delete an interest on a collaboration
router.delete('/api/project/:id/apply', auth, async (req, res) => {
const project = await Project.findOne({ _id: req.params.id, isOpen: true })
if (!project) {
res.status(500).send({ "message": "you cannot show interest at the moment" })
}
try {
project.interest = project.interest.filter((objid) => {
return objid != req.user._id
})
await project.save()
console.log(project);
return res.status(204).send(project)
} catch (e) {
res.status(500).send()
}
})
below is the project I am trying to remove a users object id from the interest
{
"isShowcase": false,
"isCollab": true,
"media": [],
"skillsNeeded": [
"5f9fc49a0282a127e4aca0fa",
"5faa596e9d54a92ab805a7a9"
],
"isOpen": true,
"interest": [
"5fa35ac783fe7042fcbbf12a"
],
"_id": "5fb98a36cc3b62aa788adbe0",
"title": "American gods",
"description": "it a story about a war between new and old gods",
"thumbnail": "https://images-na.ssl-images-amazon.com/images/S/pv-target-images/fc3ef790bcbc143d2b5b76b454b7cdcc68a19d60f629e328121713590294a895._RI_V_TTW_.jpg",
"owner": "5fa4f0af4a7bf5471c41e225",
"contributors": [],
"createdAt": "2020-11-21T21:44:22.818Z",
"updatedAt": "2020-11-22T08:22:02.377Z",
"__v": 1
}
when I run the delete request to that route, I get status 204
but the project is not returned, and when I check back on the project, I still see the project with the user's objectID still in the array.
the logged-in users object
{
"skills": [],
"isEmailConfirmed": false,
"isAdmin": false,
"isActive": true,
"hasSpecialPrevilege": false,
"_id": "5fa35ac783fe7042fcbbf12a",
"fullname": "olayinka odedeyi",
"username": "yinkus",
"email": "[email protected]",
"createdAt": "2020-11-05T01:52:07.378Z",
"updatedAt": "2020-11-22T10:37:08.307Z",
"__v": 4,
"bio": "a great girl"
}
Please, what could be preventing the logged-in user id from getting removed when the delete route to do that is run?
Upvotes: 0
Views: 252
Reputation: 1318
The problem lies in how you are comparing the two objectids in filter function. You need to use .equals() method available on objectId instance.
project.interest = project.interest.filter((objid) => {
return !objid.equals(req.user._id)
})
Upvotes: 2