I'm having trouble with a Mongoose/Express problem. High level, I have Users and Shops. Users have an array of ObjectIds that correspond to Shops. I have a page with a select dropdown that lists all the Shops, a User picks a shop, and chooses to join it.Now, this updates in the database fine and the User now has an array of references to shops. When they are redirected back to their Edit page, I'd like for that select dropdown to no longer give the option to join the same shop again. So if before it contained A, B, C, D and the user joined B, the list on refresh would contain A, C, D only.I tried to use the Lodash utility .without() and .pullAllBy(). pullAllBy() seems like it is the most appropriate thing for what I want to do, but it doesn't work.Any idea what I could be doing wrong?exports.getEditUser = async (req, res, next) => { // Must be an admin or loggedInUser._id === :userId const user = await User.findById(req.params.userId).populate('shops', 'name') // get list of shops user isn't part of let shopList = await Shop.find().select('name') console.log(user.shops) shopList = _.pullAll(shopList, user.shops) console.log(shopList) res.render('user/edit-user', { pageTitle: `Edit ${user.name}`, user: user, shopList: shopList }) }
Submitted January 07, 2019 at 09:15PM by Downvotes-All-Memes
No comments:
Post a Comment