Reputation: 772
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?
Upvotes: 10
Views: 17446
Reputation:
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.
Upvotes: 2
Reputation: 772
I found the problem: I added 'Content-Type': 'multipart/form-data'
in the headers when I post the request from my Angular form.
Upvotes: 2
Reputation: 600141
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)}`);
Upvotes: 9