简体   繁体   English

如何在异步谷歌云 function 中调用第二个 function

[英]How to call a second function within async google cloud function

I'm trying to use the firebase admin SDK to add users with specific roles.我正在尝试使用 firebase admin SDK 添加具有特定角色的用户。 I can get it working where newly added users are authenticated, but I also need to create a users/id record in my firestore database.我可以在对新添加的用户进行身份验证的地方使用它,但我还需要在我的 firestore 数据库中创建一个用户/id 记录。

Here is my code:这是我的代码:

exports.createUser = functions.https.onCall(async (data, context) => {
  const user = await admin.auth().createUser({
    email: data.email,
    emailVerified: true,
    password: data.password,
    displayName: data.displayName,
    disabled: false,
  });
  await admin.firestore().collection('users').doc(user.uid).set({
    displayName: user.displayName,
    id: user.uid,
    email: user.email,
    role: 'clientAccess',
    created: fb.firestore.FieldValue.serverTimestamp()
  })
  return { response: user }
});

Where can I put the return admin.firestore().col... part to make this work?我可以在哪里放置 return admin.firestore().col... 部分来完成这项工作?

Thanks!谢谢!

I'm going to suggest that you're not quite asking the right question.我要建议你问的问题不对。 Your task at hand is this:你手头的任务是:

  1. Create the user account创建用户帐户
  2. Add a document to firestore with information about that user将包含该用户信息的文档添加到 Firestore
  3. Send the user object back to the calling client.将用户 object 发送回调用客户端。

You don't need at all return for task 2. You just need a return for step 3. You can simply use async/await on each of the functions that return a promise to make sure they are all executed and completed in the right order before the function returns the final result to the client.您根本不需要任务 2 的return 。您只需要步骤 3 的返回。您可以简单地对每个返回 promise 的函数使用 async/await 以确保它们全部执行并以正确的顺序完成在 function 将最终结果返回给客户端之前。

    const user = await admin.auth().createUser({ ... });
    await admin.firestore().collection('users').doc(user.uid).set({ ... })
    return { response: user }

You probably also don't nee to worry about try/catch unless you have some special error handling.除非您有一些特殊的错误处理,否则您可能也不必担心 try/catch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Google Cloud function 中调用 shell 脚本 - Call shell script from within Google Cloud function 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function 如何从命令行调用 Google Cloud Function - How to Call Google Cloud Function From the Command Line 谷歌云 Function:如何继续超时 function - Google Cloud Function : how to continues timeout function 云函数:在第一个函数完成时调用第二个函数 - Cloud functions: call a second function when the first has finished 在其他 function 中调用异步 function - Call async function in other function 如何在Typescript中调用这种类型的async function - How to call this type of async function in Typescript 如何使用 API 云网关 Function 传递的服务帐户的 884645589888 在云中初始化 Google BigQuery 客户端 Function - How to initialize a Google BigQuery Client within a Cloud Function using the JWT of a service account passed by API Gateway to the Cloud Function 如何在云 function 中调用 API? - How to call an API inside a cloud function? Firebase 在手机上如何验证对云 function 的调用? - Firebase on mobile how to authenticate call to cloud function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM