-1

I'm new in all this, so please understand me. I trying to send data from HTML page (made with Google Cloud Shell) to Node.js page (made with Google Functions) but when I use the command console.log(req.body), I always see in the logs undefined. Here is the code

HTML :  
<form action="link" method="post">
 ID : <input type="text" id="demo3" name="id"><br>
<br>
Nome : <input type="text" id="demo" name="fname"><br>
<br>
Cognome : <input type="text"  id="demo1" name="lname"><br>
<br>
Età : <input type="text"  id="demo2" name="eta"><br>
<br>
<input type="submit"  value="Submit"> 
</form>

Blockquote

Node.js :
exports.helloWorld = (req, res) => {
  console.log(req.body.lname);
  console.log(req.body);
};

Blockquote

UPDATE but still doesn't work

exports.helloWorld = (req, res) => {
    var express = require('express')
  var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true }))
    console.log(req.body.fname);
  console.log(req.headers['content-type']);
    res.send(req.headers);
}; 
Miguel
  • 956
  • 6
  • 20
MattAbu
  • 1
  • 1
  • 1
    The HTML is missing a form. The JavaScript is missing any indication about what `req` is (is it an Express request object? Is it an HTTP Server request object? Something else?). There's no code which would parse the body included. – Quentin Aug 12 '19 at 09:18
  • I will update the code with form. About the req, I want to see on the console logs the data that I put on the html page – MattAbu Aug 12 '19 at 09:37
  • I see that you want to have a function in Cloud Functions that will be called by the form onSubmit. There is no need to install express.js nor use the body-parser middleware since Google Cloud Functions handle these intricacies. For example, my Cloud Function is written as such `exports.printReq = (req, res) => { let message = req.body.test; res.status(200).send(message); };` Can you provide more information on how you are calling the Cloud Function? A code snippet of the function triggering the Cloud Function would be very insightful. – JKleinne Aug 12 '19 at 22:23

1 Answers1

-1

This will work incase of express

const app = express();
app.use(bodyParser.urlencoded({
    extended: true
}));

updated:

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/', function (req, res) {
   console.log(req.body.fname);
   console.log(req.headers['content-type']);
   res.send(req.headers);
})

app.listen(3000)
Alwin Jose
  • 696
  • 1
  • 5
  • 13
  • Please find the updated answer. Express is a server framework, thats needs to be installed explicitly. In case you haven't Installed in the application, then please go through this https://expressjs.com/en/5x/api.html and https://www.youtube.com/watch?v=pKd0Rpw7O48&t=1221s – Alwin Jose Aug 12 '19 at 10:44