I got caught on how to delete an element from my json file in node.
Im displaying all my elements from my json file that i have made like this
var json = require('./test.json');
app.get('/courses', function(req, res) {
res.render('courses', {
title: "Hello",
name: "Fredrik",
js: json
});
});
This below is my test.json file
[
{
"id":1,"courseId":"DT162G","courseName":"Javascript-baserad webbutveckling","coursePeriod":1
},
{
"id":2,"courseId":"IK060G","courseName":"Projektledning","coursePeriod":1
},
{
"id":3,"courseId":"DT071G","courseName":"Programmering i C#.NET","coursePeriod":2
}
]
And now i want to be able to delete singel elements that i have looped through in my view
I made this so far but im stuck. And how should the path be in my view?
<td><a href="/courses/:id">Delete</a></td>
app.delete('/courses/:id', function(req, res) {
if(json.length <= req.params.id) {
res.statusCode = 404;
return res.send('Error 404: No quote found');
}
json.splice(req.params.id, 1);
});