Home > Software design >  How to update only specific JSON object properties with PUT router, and the other remain the same?
How to update only specific JSON object properties with PUT router, and the other remain the same?

Time:12-02

router.put('/books/:id',(req,res)=>{
    const {id}=req.params;
    const {
        isbn,
        title,
        subtitle,
        author,
        published,
        publisher,
        pages,
        description,
        website     
    }=req.body;

    booksDirectory.forEach((book,index)=>{
        if(book.isbn===id){
            booksDirectory[index]={
                isbn:`${id}`,// if isbn is not provided in req body, book doesnt exist
                title,
                subtitle,
                author,
                published,
                publisher,
                pages,
                description,
                website 
            };
        }
      
    });
   res.send(`The book with ID ${id} has been updated!`)
  
})

Im trying to update a book inside a books object by providing the book info in the PUT request body. If i provide only one property, the object will have only that one property, and the rest of the properties will not exist when i try to get that book. I want the object properties to be updated only with the properties provided, and the remaining properties to stay the same(if new ones are not provided).

CodePudding user response:

You want to update an object from array of objects of books you need to do this:

const {id}=req.params;
    const {
        isbn,
        title,
        subtitle,
        author,
        published,
        publisher,
        pages,
        description,
        website     
    }=req.body;

bookDict.forEach((element, index, array) => {
    if(element.isbn==isbn){
            if(title){
                element.title=title;
            }
            if(subtitle){
                element.subtitle=subtitle;
            }
            if(author){
                element.author=author;
            }
            if(published){
                element.published=published;
            }
            if(publisher){
                element.publisher=publisher;
            }
            if(pages){
                element.pages=pages
            }
            if(description){
                element.description=description;
            }
            if(website){
                element.website=website;
            }
        }
});

and make sure you bookDict should follow the structure below:

var bookDict = [
    {
        "isbn": 1,
        "title": "Book1",
        "subtitle": "book1",
        "author": "Ali",
        "published": "Ali",
        "publisher": "Ali",
        "pages": "100",
        "description": "Book",
        "website": "www.book.com"  
    },
    {
        "isbn": 2,
        "title": "Book1",
        "subtitle": "book1",
        "author": "Ali",
        "published": "Ali",
        "publisher": "Ali",
        "pages": "100",
        "description": "Book",
        "website": "www.book.com" 
    },
    {
        "isbn": 3,
        "title": "Book1",
        "subtitle": "book1",
        "author": "Ali",
        "published": "Ali",
        "publisher": "Ali",
        "pages": "100",
        "description": "Book",
        "website": "www.book.com" 
    }
];

and test the results by consoling the bookDict:

bookDict.forEach((element, index, array) => {
    console.log(element.title)
});
  • Related