Home Using PUT method to update existing Article in external database
Reply: 0

Using PUT method to update existing Article in external database

user2550 Published in June 23, 2018, 11:40 pm

So to cut long story short, we are Using restdb.io on a school project to practice our "back-end" skills. We are supposed to build a simple website which will allow us to post new articles, delete existing articles and (my focus point) edit existing articles. We are using the POST method for the new Articles and PUT method for the edit. Because I don't understand the nature of the PUT method, I am simply using it in the same manner as the POST method

    document.querySelector('button[name="save-edit"]').addEventListener("click", e => {
        let userTitle = document.querySelector('input[name="title"]');
        let userDate = document.querySelector('input[type="date"]');
        let userContent = document.querySelector('textarea[name="content"]');
        let data = {
            title: userTitle.value,
            content: userContent.value,
            date: userDate.value

        let postData = JSON.stringify(articleData);
        fetch("https://someRandomDatabase.restdb.io/rest/articles/" + articleData._id, {
                method: "put",
                headers: myHeaders,
                body: postData
            .then(d => d.json())
            .then(t => {
                document.querySelector("#modal").style.left = "100vw";

                document.querySelector('input[name="title"]').value = "";
                document.querySelector('input[type="date"]').value = "";
                document.querySelector('textarea[name="content"]').value = "";
                document.querySelector('button[name="save-edit"]').disabled = true;

This is the entire piece of code that takes care of the editing UX function of the website (main part starting with fetching the data). Now because I don't have that much theory regarding the PUT and all the articles I found on the Internet are just POST vs PUT, I am not sure where It's wrong. In it's current state It just adds new Article to the database (so basically just like POST method) instead of editing the one I clicked on.

PS: this piece of code is actually inside a function called showArticle(), which is dedicated to posting a new article. The reason I put in is that I am passing along the id of the Article (thus the content)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309581 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO