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);
};
if (err)- as in if there's an error. Your logic is backwards. Log what's in the error.removecallback. Right now,res.json({message: 'Member...fires regardless of whether there is an error or not.