0

here's my code for setting up PATCH request in node.js

app.patch('/api/v1/tours/:id', (req, res) => {
    const id = req.params.id * 1;
    const tour = tours.find(tour => tour.id === id);

    if (!tour) {
        return res.status(404).json({
            status: 'fail',
            message: 'Invalid ID'
        });
    }
    
    const updatedTour = {...tour, ...req.body};
    const updatedTours = tours.map(tour => tour.id===updatedTour.id ? updatedTour : tour);

    fs.writeFile(`${__dirname}/dev-data/data/tours-simple.json`, 
    JSON.stringify(tours), 
    err => {
        res.status(200).json({
            status: 'success',
            data : updatedTour
        });
    })
    
});

But when I did the patch request and clicked send in Postman, it didn't work at all patch url

Could someone help me with this? I checked every syntax and common slashes error in the URL but everything was fine. I'm using Express.js for this.

  • I just copied your code over to a codesandbox and made a patch request and got the response I expected: `{ "status": "fail", "message": "Invalid ID" }` I think your issue is that the URL you're using is wrong. Maybe prefix with `https://` or `http://` or use `http://localhost` - it's difficult to assume without seeing the app running. – Luke Garrigan Oct 03 '21 at 07:13
  • what's exactly happening in your postman? Do you get error 404? Does it never receive any answer? What do you send in postman? Did you add the json middleware to your express app? – Kamil Janowski Oct 03 '21 at 08:17
  • as y'all can see in the embedded link "patch URL". The error is 404 not found. But yes @LukeGarrigan I'll try to change that – Cassie Nguyen Oct 04 '21 at 20:53

0 Answers0