0

I am getting undefined on req.body.numbers. How can we get the value of textarea in nodejs.

<form method="POST" action="/messaging">
    <h4>Mobile Numbers</h4>
    <textarea id="numbers" class="number-area" name="numbers"></textarea>
    <h4>Add a Message</h4>
    <textarea id="message" class="message-area" name="message"></textarea>
    <br>
    <div>
        <button class="btn btn-outline-success" type="submit">
            Send
        </button>
    </div>
</form>
exports.postMessaging = (req, res, next) => {
  const numbers = req.body.numbers;
  console.log(numbers);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0
  1. You should use a <form>
  2. You also need a button to submit the form.
  3. You can catch value of <textarea> or <input> with express and body-parser.

const express = require("express");
const app = express();
const bodyParser = require("body-parser");

const port = 3000;

app.use(bodyParser.urlencoded({extended: false}));

app.post("/link", (req,res) => {
  console.log(req.body.numbers);
  // body came with body-parser
  // numbers is name of textarea
});

// req is request and res is response

app.listen(port, () => {
  console.log("server launched");
});
<h4>Mobile Numbers</h4>

<form action="/link" method="POST">
  <textarea id="numbers" class="number-area" name="numbers"></textarea>
  <button type="submit">Submit</button>
</form>
doğukan
  • 23,073
  • 13
  • 57
  • 69
0

This post will help you to understand, how to send data from HTML to node.js code.

Edited

App.js

const express = require('express');
const bodyParser = require('body-parser');
const app = exports.module = express();
app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/test', function(req, res) {
  var lName = req.body.lname;
  var number = req.body.numbers;
  res.send();
})

app.listen(1337, function() {
  console.log('App is running on 1337');
})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="http://localhost:1337/test" method="POST">
        <input name="lname" id="lname"/>
        <textarea id="numbers" class="number-area" name="numbers"></textarea>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

package.json - for your reference

"body-parser": "^1.19.0",
"express": "^4.17.1"

The above code is working and accepting textarea numbers id in req.body.numbers. The urlencoded extended value is true extended: true not extended: false

Community
  • 1
  • 1
Ajit More
  • 102
  • 3