Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3791

Call cloud function from angular app with parameters

I have Angular + Firebase application.

In my Cloud Functions I have next func:

export const getUserByEmail = functions.https.onRequest((request, response) => {
  const email = request.body.email

  admin.auth().getUserByEmail(email)
    .then(user => {
      response.send(user)
    })
    .catch(error => {
      response.status(500).send(error)
    })
})

And in Angular Service I want to call it:

  getUserByEmail(email) {
    const getUserData = this.fireFunction.httpsCallable('getUserByEmail')

    return getUserData({ request.body.email: email })
  }

Is it correct way, how it's work? Or how should I call function with parameters?

Upvotes: 0

Views: 1764

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

You are actually mixing up HTTP Cloud Functions and Callable Cloud Functions.

You Cloud Function code corresponds to an HTTP one (functions.https.onRequest(...)) but the code in your front-end calls a Callable one (const getUserData = this.fireFunction.httpsCallable('getUserByEmail')).

You should adapt one or the other, most probably adapt your Cloud Function to a Callable one, along the following lines:

exports.getUserByEmail = functions.https.onCall((data, context) => {
  const email = data.email;

  return admin.auth().getUserByEmail(email )
  .then(userRecord => {
        const userData = userRecord.toJSON();
        return { userData: userData };
  })
});

Upvotes: 3

Related Questions