mortymas
mortymas

Reputation: 11

Delete request in React with Axios

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);
};

Upvotes: 1

Views: 4332

Answers (2)

Dani
Dani

Reputation: 11

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

Upvotes: 1

Onyekachi Ezeoke
Onyekachi Ezeoke

Reputation: 906

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

Upvotes: 0

Related Questions