VAlexander
VAlexander

Reputation: 86

Returning 2 values from onCreate() cloud function in Firestore?

I'm trying to create the following two documents in different nodes from a cloud function, but not sure how to return both.

// Listen to .onCreate trigger
exports.createUserAndProfile = functions.auth.user().onCreate((user) => {

  // Create a new user (only the user themselves can access this)
  const newUser = admin.firestore().doc(`/users/${user.uid}`).set({
    announcements: [],
    email: user.email,
    onboardingSteps: ["setName", "syncStuff"],
  });

  // Create the user's public profile (any user can access this)
  const newPublicProfile = admin.firestore().doc(`/profiles/${user.uid}`).set({
    firstName: null,
    lastName: null,
    preferredName: null,
  });

  return newUser;
});

Upvotes: 1

Views: 173

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If you want both writes to complete before terminating the Cloud Function, you can return them in a Promise.all() call:

return Promise.all(newUser, newPublicProfile);

Also see the MDN documentation for Promise.all.

Upvotes: 1

Related Questions