Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3641

How to send patch request in Mongodb using node JS when there is no frontend? [closed]

$
0
0

I need to develop an backend service of sending details of comic book. The project only has backend and no frontendI have made APIs for adding , getting and deleting the dataBut i don't know how to edit details using patch request when there is no frontendMy idea is to send request like this->

localhost3000/editcomic/:_comicID/{send the body}

The body is the input in Postman which i want to sendLike if i want to change the year then i will send

{"year" : "1960}}

By sending this only year will change and nothing elseHow can i do this?

comic_routes.js file

const express = require('express');const app = express();const router = express.Router();const mongoose = require("mongoose");const ComicModel = mongoose.model("ComicModel");const bodyparser = require('body-parser');const { ObjectId } = require('mongodb');/*assuming an express app is declared here*/app.use(bodyparser.json());app.use(bodyparser.urlencoded({extended: true}));router.post("/addcomic",  (req, res) => {    const { book, author, year , price , discount , pages , condition , description , comments } = req.body;    if (!book || !author || !year) {        return res.status(400).json({ error: "One or more mandatory fields are empty" });    }    const comicObj = new ComicModel({ book: book, author: author, year: year , price : price , discount: discount        , pages : pages , condition : condition , description : description , comments : comments});    comicObj.save()        .then((newComic) => {            res.status(201).json({ comic: newComic });        })        .catch((error) => {            console.log(error);        })});router.get("/getcomics", (req, res) => {    ComicModel.find()               .then((dbComics) => {            res.status(200).json({ comics: dbComics })        })        .catch((error) => {            console.log(error);        })});router.delete("/deletecomic/:comicId",  (req, res) => {    ComicModel.findOne({ _id: req.params.comicId })        .populate("_id")        .exec((error, comicFound) => {            if (error || !comicFound) {                return res.status(400).json({ error: "Comic does not exist" });            }                comicFound.remove()                    .then((data) => {                        res.status(200).json({ result: data });                    })                    .catch((error) => {                        console.log(error);                    })        })});module.exports = router;

Viewing all articles
Browse latest Browse all 3641

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>