0

I'm having trouble accessing data from a form. As far as I can tell the following should allow me to read the username and password from the HTML form but, it is not working as expected.

Any help is appreciated.

Handlebars form ...

<form method="POST" action="/login" enctype="application/x-www-form-urlencoded">
            <table>
                <tr>
                    <td><input name="username" type="text" placeholder="User name" 
required autocomplete="username"/></td>
                </tr>

                <tr>
                    <td><input name="password" type="password" placeholder="Password" 
required autocomplete="current-password"/></td>
                </tr>

                <tr>
                    <td><input type="submit" value="Ok"/></td>
                </tr>
            </table>
        </form>

...

Express controller

...

app.post("/login", (req, res) => {
    //Prepare login request
    const email = req.query.username
    const pass = req.query.password
    
    //Send login request
    request.post("http://localhost:3000/users/login?email=" + email + "&password=" + 
     pass, function (error, restResponse, body){
    
     //Handle response
        if(error){
            return res.status(400).send(error)
        }
        const user = body.user
        const token = body.token
    
        console.log("User: " + user + " Token: " + token)
        return res.render("admin/index")
    })   
})

...

  • You should just use `req.body` instead of `req.query`. The last one is used mostly for 'GET' requests, and you're doing `POST` one. – Denys Rybkin Jun 09 '21 at 10:38

2 Answers2

1

You need to use req.body instead of req.query to access email and password from form body since the content-type is form data.

Also, it is bad practice to send email and password as query params even if used within trusted sources. This is because it is a general practice to log all request and thus not a good approach to have essentials details within query which could be logged.

Karan Gaur
  • 809
  • 7
  • 20
  • When I use "req.body.username" I get "Cannot get property 'username' of undefined" – L. Blommers Jun 09 '21 at 11:40
  • 'Query-params' can perfectly be used with 'post' request. They are even commonly use together. – Jerome Jun 09 '21 at 12:28
  • @L.Blommers could you try updating your content-type to "multipart/form-data" and try again? – Karan Gaur Jun 09 '21 at 12:47
  • @L.Blommers this shoudl further clear the reason why you are not getting data with urlencoded - https://stackoverflow.com/a/42129247/9964717 – Karan Gaur Jun 09 '21 at 12:53
  • So I've tried using "req.body" and changed the content-type to "multipart/form-data" but now I get "Cannot get property 'username' of undefined" – L. Blommers Jun 09 '21 at 13:18
  • 1
    you are not using body parser that is why you are getting undefined.use this `app.use(express.urlencoded({ extended: false }))` in your app.js – Jatin Mehrotra Jun 09 '21 at 14:10
  • https://stackoverflow.com/questions/68117682/form-data-field-value-is-undefined-null-in-nodejs – Sarah Jun 24 '21 at 14:41
0

Thank you to @Jatin Mehrotra his solution solved my problem here's the quote: "you are not using body parser that is why you are getting undefined.use this app.use(express.urlencoded({ extended: false })) in your app.js – Jatin Mehrotra "