Axios.delete is not working the way I want it too
up vote
0
down vote
favorite
It's a gym app which when a user books themselves into a class, the class saves the userId as a user which will be attending, then also in the user model you also get the classes in which the user is attending too.
Currently hitting 500 (Internal Server Error).
These are the axios calls:
deleteClassHandler = () => {
this.deleteUserClassHandler();
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/classes/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
deleteUserClassHandler = () => {
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/auth/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
The this.props.userID and this.props.id are populated fine with the right values.
These are the routes -
Classes routes:
router.delete('/remove', ClassesController.deleteUser);
Auth routes:
router.delete('/remove', UserController.deleteClass);
This are the controllers:
Classes controller -
exports.deleteUser = (req, res) => {
console.log('cl userid ', req.body.userId);
console.log('cl classid ', req.body.classId);
GymClass.findById({
_id: req.body.classId
}, 'classMembers', (err) => {
if (err) {
console.log('class up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"classMembers.userId" : mongoose.Types.ObjectId(req.body.userId)
}, (err) => {
if(err) {
console.log('class down');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
Auth controller -
exports.deleteClass = (req, res) => {
console.log('auth userid', req.body.userId);
console.log('auth classid', req.body.classId);
User.findById({
_id: req.body.userId
}, 'bookedClasses', (err) => {
if (err) {
console.log('auth up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"bookedClasses.classId" : mongoose.Types.ObjectId(req.body.classId)
}, (err) => {
if(err) {
console.log('auth down here');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
I am by no means a backend superstar and I have hit a brick wall with this one, does anyone here know how I could maybe possibly change the code and the way I am tackling this? Any issues spotted? I have got a 500 server error and I am not sure what to do. I can always post the two models for the user and classes if needed.
This was also something I tried but did not work -
axios.delete('/api/classes/remove', {
data: {
userId: this.props.userId,
classId: this.props.id
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
javascript express axios
add a comment |
up vote
0
down vote
favorite
It's a gym app which when a user books themselves into a class, the class saves the userId as a user which will be attending, then also in the user model you also get the classes in which the user is attending too.
Currently hitting 500 (Internal Server Error).
These are the axios calls:
deleteClassHandler = () => {
this.deleteUserClassHandler();
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/classes/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
deleteUserClassHandler = () => {
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/auth/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
The this.props.userID and this.props.id are populated fine with the right values.
These are the routes -
Classes routes:
router.delete('/remove', ClassesController.deleteUser);
Auth routes:
router.delete('/remove', UserController.deleteClass);
This are the controllers:
Classes controller -
exports.deleteUser = (req, res) => {
console.log('cl userid ', req.body.userId);
console.log('cl classid ', req.body.classId);
GymClass.findById({
_id: req.body.classId
}, 'classMembers', (err) => {
if (err) {
console.log('class up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"classMembers.userId" : mongoose.Types.ObjectId(req.body.userId)
}, (err) => {
if(err) {
console.log('class down');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
Auth controller -
exports.deleteClass = (req, res) => {
console.log('auth userid', req.body.userId);
console.log('auth classid', req.body.classId);
User.findById({
_id: req.body.userId
}, 'bookedClasses', (err) => {
if (err) {
console.log('auth up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"bookedClasses.classId" : mongoose.Types.ObjectId(req.body.classId)
}, (err) => {
if(err) {
console.log('auth down here');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
I am by no means a backend superstar and I have hit a brick wall with this one, does anyone here know how I could maybe possibly change the code and the way I am tackling this? Any issues spotted? I have got a 500 server error and I am not sure what to do. I can always post the two models for the user and classes if needed.
This was also something I tried but did not work -
axios.delete('/api/classes/remove', {
data: {
userId: this.props.userId,
classId: this.props.id
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
javascript express axios
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it aPUTand deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?
– Andrew
Nov 8 at 13:55
1
Maybe you wan to put it into the uri as param, sth like/api/users/:userId/classes/:classId. Also/removeis redundant, since in a RESTful call, the methodDELETEis already hinting its use
– William Chong
Nov 8 at 16:02
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
It's a gym app which when a user books themselves into a class, the class saves the userId as a user which will be attending, then also in the user model you also get the classes in which the user is attending too.
Currently hitting 500 (Internal Server Error).
These are the axios calls:
deleteClassHandler = () => {
this.deleteUserClassHandler();
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/classes/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
deleteUserClassHandler = () => {
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/auth/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
The this.props.userID and this.props.id are populated fine with the right values.
These are the routes -
Classes routes:
router.delete('/remove', ClassesController.deleteUser);
Auth routes:
router.delete('/remove', UserController.deleteClass);
This are the controllers:
Classes controller -
exports.deleteUser = (req, res) => {
console.log('cl userid ', req.body.userId);
console.log('cl classid ', req.body.classId);
GymClass.findById({
_id: req.body.classId
}, 'classMembers', (err) => {
if (err) {
console.log('class up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"classMembers.userId" : mongoose.Types.ObjectId(req.body.userId)
}, (err) => {
if(err) {
console.log('class down');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
Auth controller -
exports.deleteClass = (req, res) => {
console.log('auth userid', req.body.userId);
console.log('auth classid', req.body.classId);
User.findById({
_id: req.body.userId
}, 'bookedClasses', (err) => {
if (err) {
console.log('auth up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"bookedClasses.classId" : mongoose.Types.ObjectId(req.body.classId)
}, (err) => {
if(err) {
console.log('auth down here');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
I am by no means a backend superstar and I have hit a brick wall with this one, does anyone here know how I could maybe possibly change the code and the way I am tackling this? Any issues spotted? I have got a 500 server error and I am not sure what to do. I can always post the two models for the user and classes if needed.
This was also something I tried but did not work -
axios.delete('/api/classes/remove', {
data: {
userId: this.props.userId,
classId: this.props.id
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
javascript express axios
It's a gym app which when a user books themselves into a class, the class saves the userId as a user which will be attending, then also in the user model you also get the classes in which the user is attending too.
Currently hitting 500 (Internal Server Error).
These are the axios calls:
deleteClassHandler = () => {
this.deleteUserClassHandler();
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/classes/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
deleteUserClassHandler = () => {
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/auth/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
The this.props.userID and this.props.id are populated fine with the right values.
These are the routes -
Classes routes:
router.delete('/remove', ClassesController.deleteUser);
Auth routes:
router.delete('/remove', UserController.deleteClass);
This are the controllers:
Classes controller -
exports.deleteUser = (req, res) => {
console.log('cl userid ', req.body.userId);
console.log('cl classid ', req.body.classId);
GymClass.findById({
_id: req.body.classId
}, 'classMembers', (err) => {
if (err) {
console.log('class up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"classMembers.userId" : mongoose.Types.ObjectId(req.body.userId)
}, (err) => {
if(err) {
console.log('class down');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
Auth controller -
exports.deleteClass = (req, res) => {
console.log('auth userid', req.body.userId);
console.log('auth classid', req.body.classId);
User.findById({
_id: req.body.userId
}, 'bookedClasses', (err) => {
if (err) {
console.log('auth up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"bookedClasses.classId" : mongoose.Types.ObjectId(req.body.classId)
}, (err) => {
if(err) {
console.log('auth down here');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
I am by no means a backend superstar and I have hit a brick wall with this one, does anyone here know how I could maybe possibly change the code and the way I am tackling this? Any issues spotted? I have got a 500 server error and I am not sure what to do. I can always post the two models for the user and classes if needed.
This was also something I tried but did not work -
axios.delete('/api/classes/remove', {
data: {
userId: this.props.userId,
classId: this.props.id
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
javascript express axios
javascript express axios
edited Nov 8 at 14:49
asked Nov 8 at 13:42
Andrew
17911
17911
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it aPUTand deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?
– Andrew
Nov 8 at 13:55
1
Maybe you wan to put it into the uri as param, sth like/api/users/:userId/classes/:classId. Also/removeis redundant, since in a RESTful call, the methodDELETEis already hinting its use
– William Chong
Nov 8 at 16:02
add a comment |
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it aPUTand deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?
– Andrew
Nov 8 at 13:55
1
Maybe you wan to put it into the uri as param, sth like/api/users/:userId/classes/:classId. Also/removeis redundant, since in a RESTful call, the methodDELETEis already hinting its use
– William Chong
Nov 8 at 16:02
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it a
PUT and deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?– Andrew
Nov 8 at 13:55
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it a
PUT and deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?– Andrew
Nov 8 at 13:55
1
1
Maybe you wan to put it into the uri as param, sth like
/api/users/:userId/classes/:classId. Also /remove is redundant, since in a RESTful call, the method DELETE is already hinting its use– William Chong
Nov 8 at 16:02
Maybe you wan to put it into the uri as param, sth like
/api/users/:userId/classes/:classId. Also /remove is redundant, since in a RESTful call, the method DELETE is already hinting its use– William Chong
Nov 8 at 16:02
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208978%2faxios-delete-is-not-working-the-way-i-want-it-too%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I believe you need to have your data in the config object axios.delete('someURL', { data: { userId: this.userId, classId: this.class.id}}) The delete method doesn't have a dedicated data parameter like PUT, PATCH, and POST. axios.delete(url, config) opposed to axios.post(url, data, config)
– Budhead2004
Nov 8 at 13:52
@Budhead2004 That is what I was reading but I still struggled to put it into my code, so which way would you tackle it? I was thinking of making it a
PUTand deleting it via that but I did not think that was the right way so I didn't do it. How would you work around it?– Andrew
Nov 8 at 13:55
1
Maybe you wan to put it into the uri as param, sth like
/api/users/:userId/classes/:classId. Also/removeis redundant, since in a RESTful call, the methodDELETEis already hinting its use– William Chong
Nov 8 at 16:02