I am currently developing a mean stack application and encountering difficulties when attempting to send requests to the Express/Node server in order to delete an element from an array in Mongo. Below is my schema for reference:
var DeckSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Deck name',
trim: true
},
card: [{
name: {
type: String, default: '',
trim: true,
required: 'Please fill Card name'
},...
Provided below is the client-side route I have set up:
.state('decks.card', {
url: '/:deckId/card/:cardId',
templateUrl: 'modules/decks/client/views/view-card.client.view.html',
controller: 'DecksController',
controllerAs: 'vm',
resolve: {
deckResolve: getDeck,
cardResolve: getCard
},...
function getDeck($stateParams, DecksService) {
return DecksService.get({
deckId: $stateParams.deckId
}).$promise;
}
function getCard($stateParams, RemoveCardService) {
return RemoveCardService.get({
deckId: $stateParams.deckId,
cardId: $stateParams.cardId
}).$promise;
Below are the details of the server-side routes:
app.route('/api/decks/:deckId/card/:cardId').all()
.get(decks.read)
.put(decks.update)
.delete(decks.deleteCard);
// Finish by binding the Deck middleware
app.param('deckId', decks.deckByID);
//app.param('cardId', decks.cardByID); This causes errors
Also, here is the server-side controller code snippet:
exports.deleteCard = function(req, res) {
res.send('In deleteCard');
Deck.update(
{'_id': req.body.deck_.id},
{ $pull: { "card" : { id: req.body.deck.card._id } } },
false,
true
);
//decks.update( {id: req.params.deckId}, { $pullAll: {id: [req.params.cardId] } } )
};
Additionally, included is the middleware logic:
exports.cardByID = function(req, res, next, id){
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
message: 'Card is invalid'
});
}
Deck.findById(id).populate('card','user', 'displayName').exec(function (err, deck) {
if (err) {
return next(err);
} else if (!deck) {
return res.status(404).send({
message: 'No Card with that identifier has been found'
});
}
req.deck.card = deck.card;
next();
});
};
I am struggling to execute the deleteCard method in the client controller. The expected "In DeleteCard" message is not being displayed in the console, and I am unable to identify the issue. If further information is required, please let me know. Thank you in advance!