3

I am making an http request from an angular form like that:

  this.http.post(this.endPoint, formData, { responseType: 'text' }).subscribe(
    res => {
      console.log(res);
    }
  )

And I have a simple cloud function:

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });

exports.test = functions.https.onRequest((req, res) => {

    cors(req, res, () => {
        const data = req.body;
        res.send(`Hello from Firebase! ${data}`);
    });

})

However the req.body does not work and i get this response :

Hello from Firebase! [object Object]

Is there any idea why this happens?

Giannis Savvidis
  • 642
  • 1
  • 9
  • 26

3 Answers3

5

If you're trying to print the req.body value, you first have to convert it from a JavaScript object to a string:

res.send(`Hello from Firebase! ${JSON.stringify(data)}`);
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
2

Frank's answer is probably closer to what you are looking for.

Alternatively, if you're wanting to just print specific properties:

You are using the template literal to inject the req.body into your string. Since req.body (or data in this case) is an object, you'll have to pull the value(s) out of it that you want to display like req.body.prop.

This example from Firebase quickstart shows pulling the property off the request body.

1

I found the problem: I added 'Content-Type': 'multipart/form-data' in the headers when I post the request from my Angular form.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Giannis Savvidis
  • 642
  • 1
  • 9
  • 26