1

I'm trying to make a delete request in React with Axios. My backend is node. I'm having trouble passing the data properly through axios. It sucessfully console.log(res) and hits the controller giving me the Member sucessfully deleted message. But when i refresh my page, the record is still there. I don't know what is causing that. This is my axios code in react:

onDelete(memberId) {
    axios.delete('http://localhost:5000/api/members', {id: memberId})
        .then(res => {
            console.log(res)
            console.log('it works')
        })
        .catch(function (error) {
            console.log(error);
        });
}

this is how i'm calling it:

<Table.Body>
    {this.state.members.map(member =>
        <Table.Row key={member._id}>
            <Table.Cell>{member.name}</Table.Cell>
            <Table.Cell>{member.email}</Table.Cell>
            <Table.Cell> <Button icon='remove user' onClick={() => this.onDelete(member._id)}/></Table.Cell>
        </Table.Row>
    )}
</Table.Body>

this is my controller:

exports.delete_a_member = function (req, res) {
    console.log(req.params);
    Member.remove({
        _id: req.params.memberId
    }, function (err, member) {
    if (err)
        res.send(err);
    res.json({message: 'Member successfully deleted!!'});
    });
};

this is my route:

module.exports = function(app) {
    var members = require('../controllers/membersController');

    app.route('/api/members')
        .get(members.list_all_members)
        .post(members.create_a_member)
        .delete(members.delete_a_member);
};
2
  • You return the message if if (err) - as in if there's an error. Your logic is backwards. Log what's in the error. Commented Oct 29, 2017 at 20:03
  • I'd provide two separate paths for the remove callback. Right now, res.json({message: 'Member... fires regardless of whether there is an error or not. Commented Oct 29, 2017 at 22:41

2 Answers 2

1

Try this http://localhost:5000/api/members${memberId} i tried and it worked, you can ask Jasser Abdel the system angel

Sign up to request clarification or add additional context in comments.

Comments

0

It seems remove() is deprecated in deleting a document from a collection in mongoDb. Try out deleteOne() method. You can checkout how to use it in the documentation https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/#db.collection.deleteOne

1 Comment

hey thanks! that's a good thing to notice. i tried it but it still isn't working for me. but that's a really good thing for me to keep in mind. thanks :-)

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.