Farah10
Farah10

Reputation: 91

Node JS PATCH request running no matter what

Sorry if the title if confusing, I wasn't too sure how to word it. I have a PATCH request to update a value in my database, but even though it is "working" (200 status), it's not actually. I have a .route('/:movie_id/:user_id').all() handler to trigger for all my methods, where it pulls a movie from the database by movie_id and user_id. This works. Then I move on to my PATCH request, but it seems like the PATCH request isn't actually running. I am getting the correct response from the .all() handler, but no update is happening. Even if I completely comment out the code for my PATCH, I am still getting a 200 status.

Here is my .all() handler with my PATCH request:

movieRouter
    .route('/:movie_id/:user_id')
    .all(requireAuth)
    .get((req, res, next) => {
        const db = req.app.get('db')
        MovieService.getById(db, req.params.movie_id, req.params.user_id)
        .then(movie => {
            if(!movie) { // this runs fine
                return res.status(404).json({ error: `Movie doesn't exist`})
            }
            // res.json({movie : movie}); --> old code
            // solution:
            res.movie = movie;
            next();
            return movie;
        })
        .catch(next)
    })
    .patch(requireAuth, (req, res, next) => {
        const db = req.app.get('db')
        const { watched } = req.body
        const updatedMovie = { watched }

        // this doesn't run
        const numVal = Object.values(updatedMovie).filter(Boolean).length
        if(numVal === 0) {
            return res.status(400).json({ error: `Must not be blank`})
        }

        MovieService.updateMovie(db, req.params.movie_id, req.params.user_id, updatedMovie)
            .then(movie => {
                res.status(200).json(updatedMovie)
            })
            .catch(next)
    })

Here is my MovieService:

updateMovie(db, movie_id, newMovie) {
        return db('your_movie_list').where('id', movie_id).where('user_id', user_id).update(newMovie).returning('*')
    }

Upvotes: 0

Views: 1053

Answers (1)

Mic Fung
Mic Fung

Reputation: 5692

It should be the problem of the 2nd .all(), .all() will catch all request, no matter it is GET, POST, PATCH, DELETE. So even when you comment out PATCH code, it will return 200.

Change the 2nd .all to .get like below


app.use(express.json())

movieRouter
    .route('/:movie_id/:user_id')
    .all(requireAuth)
    .get((req, res, next) => { // use .get instead of .all to avoid catching all requests
        const db = req.app.get('db')
        MovieService.getById(db, req.params.movie_id, req.params.user_id)
        .then(movie => {
            if(!movie) { // this runs fine
                return res.status(404).json({ error: `Movie doesn't exist`})
            }
            res.json({movie : movie});
        })
        .catch((e) => {
            console.log("From getMovie", e); 
            res.status(400).json({ error: e.message }) 
        })
    })
    .patch((req, res, next) => { 
        try {
           const db = req.app.get('db')
           const { watched } = req.body
           const updatedMovie = { watched }

        
          // this doesn't run
          const numVal = Object.values(updatedMovie).filter(Boolean).length
          if(numVal === 0) {
              return res.status(400).json({ error: `Must not be blank`})
          }

          MovieService.updateMovie(db, req.params.movie_id, req.params.user_id, updatedMovie)
            .then(movie => {
                console.log(movie) // nothing logs 
                res.status(200).json(movie[0])
            })
            .catch((e) => {
                console.log("From updateMovie", e); 
                res.status(400).json({ error: e.message }) 
            })
         }catch(e) {
             console.log("From try/catch", e); 
             res.status(400).json({ error: e.message }) 
         }
    })

A little working example for cross-checking

const express = require("express");
const app = express();
const PORT = process.env.PORT || 8080;
app.use(express.json())

const movieRouter = express.Router()
movieRouter
    .route('/:movie_id/:user_id')
    // .all(requireAuth)
    .get((req, res, next) => { 
        res.json({"movie:get" : 1});
    })
    .patch((req, res, next) => { 
        res.json({"movie:patch" : 1});
    })

app.use(movieRouter)

app.listen(PORT, function (err) {
  if (err) console.log(err);
  console.log("Server listening on PORT", PORT);
});

Upvotes: 1

Related Questions