0

I have a schema like:

Question { id, user, ... }
Answer { id, user, question, ... }

I want to allow both user who posted answer or the parent question to delete an answer. How can I do it?

I want to remove an Answer if the userId param is either Answer.user.id or Answer.question.user.id.

The current implementation looks like

Answer.destroy({ id: id, user: loggedInUserId })

And it allows only owner of answer to remove his answer. Missing the part where poster of question can delete any answer under his question

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

1 Answers1

0
Answer.destroy({ id: id, user: loggedInUserId })

Will query for Answers that are written by the user with loggedInUserId and have the given id and will destroy the one it finds. To achieve what you are trying to do, considering that you know your answer's id, you should do something like:

Answer.findOne({id: answerId}).populateAll()
.exec(function(err, answer) {
   if (answer.user.id == loggedInUserId || answer.question.user == loggedInUserId) {
    answer.destroy(callback);
   }
});

Considering that answer is the Answer instance that you want to destroy.

roign
  • 452
  • 5
  • 10