0

I am working on the following request, but when I run it in postman, it returns empty json.

router.get('/:id', (req, res) => {
    console.log(req.params.id)
    User.findById(req.params.id)
    .then((user)=> {
        console.log(user.eMail, user.words, user)
        res.json(user)})
    .catch((err) => {res.status(400).json("Error: "+err)})
})

I am able to get user.eMail, user.words, and user to all console.log. So I believe my json request in postman is working correctly. However, when I try to return the user in JSON format I get blank JSON with a 200 status. I have also tried switching my res.json() with the following

res.json({
            eMail: user.eMail,
            words: user.words
        })})

However, that has also returned me empty JSON in postman

David Reke
  • 535
  • 1
  • 5
  • 20
  • 1
    Try to replace thw whole handler with `res.json({ eMail: 'test', words: 'test words' })})` and find out the issue is gone or not – Anatoly Oct 29 '20 at 18:38
  • It never occured to me to make my json static to test it. And when I did give that a try, it also returned a blank json. – David Reke Oct 29 '20 at 18:42
  • 1
    so your issue is not related with this route. Maybe you also have `router.get('/', ...`? – Anatoly Oct 29 '20 at 18:51
  • 1
    Have you confirmed that as soon as you send the request via Postman it logs in the console? Clear terminal and then validate it. Also - not sure what your other routes look like, but Postman could potentially be configured for a POST request and not a GET request. You can also try manually entering the URL into your browser and giving that a shot. It'll return JSON right in your browser window since it's just a simple get request to a specific URL on a specific port. http://127.0.0.1:3000/idhere – Tony Drummond Oct 29 '20 at 18:56
  • Ok, I cleared my terminal, and the console.log still works. I also tried manually typing in my browser, and that actually worked – David Reke Oct 29 '20 at 19:04
  • Not sure on why Postman wasn't working...very odd. – Tony Drummond Oct 29 '20 at 19:36

1 Answers1

1

Ok, I've found the solution from a previous stack overflow thread: GET request works in browser but not in POSTMAN or SOAPUI

It turns our my response was coming through the browser but not in postman. When I googled that issue I found that I should clear out my requests in postman, clear out my cookies, and start over. Everything worked perfectly after that.

David Reke
  • 535
  • 1
  • 5
  • 20
  • Excellent and thank you for sharing. I was curious what could be going wrong. Development can be quite frustrating at times :) – Tony Drummond Oct 29 '20 at 19:48