Open Beacon24 opened 2 years ago
Or have I not properly declared my group variable so that members are accessible to the function?
I don't have a good understanding of the code so I'm not sure, but I'm guessing it's this? I don't see a function defined on group.members.updateOne
. can you point me to where this function is defined?
Hmm I may be wrong but is it not just mongoose/Mongo syntax? In the same way that
group.members.addToSet(user)
and user.groups.addToSet(group)
work in the joinGroup
function defined right above, without me ever having to define addToSet()
Or is this not the same as what you're talking about?
also, I noticed I listed the wrong file before, this is in the /controllers/groups.js
file. I've corrected the opening comment as well.
hmm, good question.. I'm not exactly sure what the issue is, I'm not super familiar with mongoose. here's how I'd debug it:
module.exports.leaveGroup = async (req, res) => {
const { id } = req.params;
const user = req.user;
const group = await Group.findById(id);
console.log(group)
/**
* You should see something like:
* group = { members: { updateOne: function(){} } }
*/
group.members.updateOne(
{ $pull: user}
);
user.groups.updateOne(
{ $pull: group}
);
await group.save();
await user.save();
req.flash('success', 'Left group!');
res.redirect(`/groups/${group._id}`);
}
See the console.log statement above. print out group
(or use a browser debugger, your preference) to inspect the group
variable. It should be an object with a property, members
. members
should have a property called updateOne
, which is a function. If it isn't, there's your problem. Unfortunately I don't know enough about mongoose to tell you why it's not there, sorry :/
Aha! Good thinking. The group object did return with the property members, but not the function, but I realize, the updateOne() function probably needs to be called on the group, and not the nested members array. I am going to look at the docs to make sure I have the syntax right and see if I can fix it. Thanks!
I've updated to what I think might be proper syntax. No more error message, but the function is still not executing properly, as the group does not get 'left'. Will continue digging. Controller file now has a better stab at proper syntax, with updateOne()
called on group
and user
instead of group.members
and user.groups
.
Here is what I think is closer to correct syntax:
module.exports.leaveGroup = async (req, res) => {
const { id } = req.params;
const user = req.user;
const group = await Group.findById(id);
group.updateOne(
{ $pull: { "group.members": user} }
);
user.updateOne(
{ $pull: { "user.groups": group} }
);
await group.save();
await user.save();
req.flash('success', 'Left group!');
res.redirect(`/groups/${group._id}`);
}
But clearly not quite correct yet.
I've been looking through the MongoDB docs but so far still unsure. Here are the docs for the $pull option: https://www.mongodb.com/docs/manual/reference/operator/update/pull/
This functionality has been temporarily hidden but it is still a problem worth working on, we will re-introduce this feature hopefully very soon.
I am trying to create a way for users to leave groups that they have joined. I have the following function in my users controller file,
/controllers/groups.js
:When I try to leave the group, I get the error message:
group.members.updateOne is not a function
Am I using the MongoDB syntax incorrectly? Or have I not properly declared my group variable so that
members
are accessible to the function?