Req.query is empty

Hello everyone, I have this following line of code:

app.get("/posts/login", (req, res) => {
console.log(req.body)
}

This line receives input from a form, like this:

<form action="/posts/login">

      <input type="text" placeholder="Username" />
      <input type="password" placeholder="Password" />
      <input type="submit" value="Log In" />
    </form>

I’ve been testing this, and this is the output:
Screen Shot 2020-08-24 at 2.03.27 PM

Whaaat? Why is it empty?

Have you added body-parser package?

1 Like

Yes.

const bodyParser = require("body-parser");
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());

Did you tried with console.log(req), there something is messing up the body of the request.

1 Like

Yeah, I did just log the req and manually read the entire output but couldn’t find anything.
Here’s the repo: (not on Glitch lol): https://repl.it/@CarlyRaeJepsenS/gli-forktery-1#server.js

Maybe there was an update to express or something?

Did you corrected the MongoDB urls, or they are correct? That may errors out.

1 Like

I just changed the MongoDB url - it works correctly now and I see an incoming connection. This error doesn’t have anything to do with my database though…

That’s me testing the Login stuff on the website and showing the console what it says.

1 Like

This link suggests changing extended to true.

Edit: nope, doesn’t work.

Weird.
Screenshot_697

1 Like

I just restarted the server, that might be why. Is that postman?

That is the request for the login, i putted test as the username and test a password and clicked Login.

1 Like

Huh, I still see a bunch of curly braces.
Screen Shot 2020-08-24 at 4.09.57 PM

This is part of the req response:
Screen Shot 2020-08-24 at 4.11.07 PM

Did you try with replacing the req.body with req.query.
(Source: Stackoverflow)

1 Like

I’m trying that right now.
Edit: I got the same thing: just an empty brace.
Edit: I’m changing extended back to false. Still doesn’t work.

Well for anyone looking at this, I have body-parser installed and it still isn’t working…
I also tried adding name attributes to the text forms but that isn’t working either.

I logged the entire req body, and adding the name attribute fixed it! Now I can use req.query to gather the input.
image

1 Like

Yay! Finally fixed all bugs!

1 Like

Hi, may I check what do you mean by “I logged the entire req body, and adding the name attribute fixed it!” ? I am having the same issue as you, my req.body is empty

My error was because my form didn’t have the “name” attribute. Add name="something" to your form, and then the Express response should be able to be accessed as req.query.something. It’s been a while since I did node so idk if that works, but I hope it does.

1 Like

ah i see, thanks!