0

In my application, I have a form with the action pointing to a nodejs endpoint. It is sucessfully performing the action, but when i try to extract the posted data from req, it refuses to give me anything. I may just be looking in the wrong place.

Here is my HTML

<body>
    <h1>Hello, you are here</h1>
    <form action="/archive_message" method="post">
        Message URL: <input type="text" name="urlToProcess">
        <select>
            <option name="process-method" value="msg">Message</option>
            <option name="process-method" value="trd">Thread</option>
            <option name="process-method" value="act">Account</option>
        </select>
        <input type = "submit" value = "Submit">
    </form>
</body>

here is my js

server.start().then(res => {
   server.applyMiddleware({app})
   app.listen(8081, function () {   
      console.log("Running!");
   })
});


app.get('/', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html");
   console.log("200 - GET");
});


app.post('/archive_message', function (req, res) {
   console.log(req.body.urlToProcess);
   res.redirect("/");
})

When i log req.body, i just get header info, when i try to dig further, i get errors. How am i supposed to be accessing the data submitted by the form?

specifically, i am getting this error: TypeError: Cannot read properties of undefined (reading 'urlToProcess')

Any help is appreciated.

6
  • 1
    are you using the bodyparser middleware? Commented Nov 14, 2022 at 20:38
  • Not sure if it's exactly the problem, but "name" applies to the "select" element, not "option". Commented Nov 14, 2022 at 20:40
  • I am not familiar with that. Do you have a link i could check out? @Barmar Commented Nov 14, 2022 at 20:40
  • @user1751825 thank you. that is not what is causing this issue, but you are correct that i need to fix that. Thank you Commented Nov 14, 2022 at 20:42
  • 1
    npmjs.com/package/body-parser Commented Nov 14, 2022 at 20:43

1 Answer 1

-1

try this one app.use(express.urlencoded({ extended: true }));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.