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









share|improve this question
























  • 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






  • 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

















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









share|improve this question
























  • 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






  • 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















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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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




















  • 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






  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings