Reputation: 49
In my cloud function, I take the JSON body and particular elements of it.
const type = request.body.type;
But what if type was optional. And the user didn't have to put it. Is there a way of doing the above without it erroring out.
Because in the instant above, if the user left "type" out of their object, then it would cause an error.
Upvotes: 0
Views: 316
Reputation: 83163
If I correctly understand your question you want to check if there is a type
property in the request
's body.
The following should do the trick:
if (request.body.type) {
//There is a type, act accordingly
} else {
//No type
}
Upvotes: 4