Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to DELETE "/api/notes" with an id

app.delete("/api/notes/:id", (req, res) => {
    let chosenNoteToDelete = req.params.id;
    fs.readFile(__dirname + "/db/db.json", (err, data) => {
        if (err) {
            console.log(err);
            res.sendStatus(500);
            return;
        }
        try {
            let json = JSON.parse(data);
        } catch(e) {
            console.log(err);
            res.sendStatus(500);
            return;
        }

        for (let i = 0; i < json.length; i++) {
            if (json[i].id === chosenNoteToDelete) {
                json.splice(i, 1);
                return;
            }
        }

        fs.writeFile(__dirname + "/db/db.json", JSON.stringify(json), (err) => {
            if (err) {
                console.log(err);
                res.sendStatus(500);
                return;
            }
            res.send("Successfully deleted");
        });
    });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: send variable data from node js to front end javascript 
Javascript :: convert json in parse inputs azure function 
Javascript :: cgi-node example 
Javascript :: query sequnce graphql 
Javascript :: selenium how to automate javascript dialogs 
Javascript :: how to check type of value in a java script file 
Javascript :: button inside popover not viible 
Javascript :: remove state from location on page load in react router dom 
Javascript :: how to show product count in jquery return response 
Javascript :: rest operator javascript 
Javascript :: parsing through json without using key value python 
Javascript :: slack icon emoji for alertmanager 
Javascript :: what is the difference between explicit parameter and rest parameter javascript 
Javascript :: u017f javascript 
Javascript :: how to replace nan with a value in js 
Javascript :: user attributes for custom elemets 
Javascript :: riverSizes javascript 
Javascript :: how to check record successfully delete in mongodb node js 
Javascript :: place white and black knights on 2x2 chessboard 
Javascript :: prevent specific state redux-persist 
Javascript :: mdn 
Javascript :: create react app cloudfront invalidation 
Javascript :: js unwrap element 
Javascript :: javascript alternance images 
Javascript :: lesson-3 
Javascript :: getcontext canvas not autocomplete 
Javascript :: textfield label language react 
Javascript :: firebase update return result 
Javascript :: gatsby markdown link blank 
Javascript :: python js 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =