Darryl
Darryl

Reputation: 49

Firebase Cloud Functions Check Value is not empty

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

Answers (1)

Renaud Tarnec
Renaud Tarnec

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

Related Questions