0

My schema looks like the following:

const userSchema = new Schema({
  ...
  followings: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }

  ],
  followers: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }
  ],
}, {timestamps: true})

And I need to implement "unfollow" feature. I am currently trying this:

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { "followings": {"_id": user._id }}
            }

            const updated = User.update(query, update)

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { "followers": {"_id": current_id} }
            }

            const secondUpdated = User.update(secondQuery, secondUpdate)

            if (!updated || !secondUpdated) {
                return res.status(404).json({ error: 'Unable to unfollow that user' })
            }

            res.status(200).json({
                update, 
                secondUpdate
            })
} 
catch (err) {
            res.status(400).send({ error: err.message })
}

This gives status 200 and sends update & secondUpdate to the client, but the actual object doesn't get deleted from the database. What is wrong with my code?

2 Answers 2

1

If you are using mongodb native drive

You should import mongodb ObjectID. Before performing operations.

const ObjectId = require('mongodb').ObjectID;

$pull: { "followers": {"_id": current_id} }

change to:

$pull: { "followers": {"_id": new ObjectId(current_id) }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I imported all the necessary stuff. The console doesn't give me any kind of error, which would have not been the case if the import was the issue.
0

Thank you for your answers. In my case, the following worked for me.

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { followings: {_id: user._id }}
            }

            const updated = User.updateOne(query, update, {
                safe: true
            }, function(err, obj) {
                console.log(err);
            })

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { followers: {_id: current_id} }
            }

            console.log(secondQuery)
            console.log(secondUpdate)

            User.updateOne(secondQuery, secondUpdate, {
                safe: true
              }, function(err, obj) {
                res.status(200).json({
                    obj
                });
              })

} 
catch (err) {
            res.status(400).json({ error: err.message })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.