1

I have a problem removing data in my Nodejs app. I have 2 buttons in my ejs template that send the exactly same URL to server to remove 2 models: the first button:

<td class="td6"><a href="/myprojects/<%= project._id %>/cost/<%= labour._id %>/remove"><button type="button" class="btn btn-danger">Remove</button></a></td>

The second button:

<td class="td21"><a href="/myprojects/<%= project._id %>/cost/<%= tool._id %>/remove"><button type="button" class="btn btn-danger">Remove</button></a></td>

I also have exactly the same route for both for removing data. The first one working perfectly but the second one doesn't work at all:

first route:

app.get("/myprojects/:id/cost/:labour_id/remove", function(req, res){
Labour.findByIdAndRemove(req.params.labour_id, function(err){
    if(err){
        console.log(err);
        }else{
            Project.findById(req.params.id, function(err, foundProject){
                if(err){
                        console.log(err);
                        }else{
                        res.redirect("/myprojects/" +req.params.id+ "/cost");
                        }
            });
    }
});

});

and the second route:

app.get("/myprojects/:id/cost/:tool_id/remove", function(req, res){
Tool.findByIdAndRemove(req.params.tool_id, function(err){
    if(err){
        console.log(err);
        }else{
            Project.findById(req.params.id, function(err, foundProject){
                if(err){
                        console.log(err);
                        }else{
                        res.redirect("/myprojects/" +req.params.id+ "/cost");
                        }
            });
    }
});

});

can anyone see any problem here?

1 Answer 1

3

It seems like you're trying to use the exact same route pattern for doing two different things (or, well, exactly same thing, but on different kinds of entities). What I mean is that for Express /myprojects/:id/cost/:labour_id/remove and /myprojects/:id/cost/:tool_id/remove are exactly same routes, so the second one simply never gets matched. The solution would be to make two different routes like /myprojects/:id/costs/labors/:labour_id/remove and /myprojects/:id/costs/tools/:tool_id/remove respectively (note /labors/ and /tools/ sections).

Another way (and that's what you were probably trying to do) would be to create a universal route like /myprojects/:id/costs/:entity(labours|tools)/:tool_id/remove and use corresponding Mongoose model based on the value of the entity parameter. However, I wouldn't recommend it, since it would make the code way less readable. Sometimes duplication is not such a bad thing. ;)

Also, as a general rule, it's a good practice to implement removal routes as methods delete methods (app.delete).

Sign up to request clarification or add additional context in comments.

1 Comment

I tried the first option and worked perfect. Thanks for your good answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.