使用Firebase函数onCall在Firestore中获取CRUD数据.其中的任何异步操作(Promise)会发生什么? [英] CRUD data in firestore using firebase function onCall. What happens to any async operation (Promise) within it?

本文介绍了使用Firebase函数onCall在Firestore中获取CRUD数据.其中的任何异步操作(Promise)会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用下面的函数时,我的Firestore没有任何反应.我的GCP日志中也看不到"helloWorld内部".

Nothing happens to my firestore when I call the function below. I also can't see "inside helloWorld" on my GCP logs.

exports.helloWorld = functions.https.onCall((data, context) => {

 console.log("inside helloWorld);
 const users = admin.firestore().collection('users');

 users.doc(data.userId).get().then( // --------------------Line A
    (snapshot) => {
      if (snapshot.exists) {
        console.log("snapshot exists");
        return null;
      } else {
        console.log("inside else");
        users.doc(data.userId).set({
          name: data.name
        });
        return null;
      }
    }
  ).catch(() => 'obligatory catch');

  return; //-----------------------------------------------Line B
});

但是,当我将退货单放在行A上时,该函数将按预期工作,并且在我的Firestore中创建了一个文档.我的GCP日志中显示"inside helloWorld".

However, when I place the return on Line A, the function works as expected and a document is created in my firestore. "inside helloWorld" is shown on my GCP logs.

为什么会这样? 我真的很感谢任何澄清.

Why is that so? I really appreciate any levels of clarification.

推荐答案

根据文档可调用函数(尤其是有关发回结果的部分) :

According to the documentation for callable functions (particularly the part about sending back a result):

要在异步操作后返回数据,请返回promise.这 承诺返回的数据将发送回客户端.例如, 您可以返回可调用函数写入的经过清理的文本 实时数据库.

To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client. For example, you could return sanitized text that the callable function wrote to the Realtime Database.

即使您不想在响应中发送任何内容,可调用函数仍然需要响应发起它们的HTTP请求,就像所有HTTP事务一样.

Even if you don't want to send any content in the response, Callable functions still need to respond to the HTTP request that originated them, as all HTTP transactions do.

无论哪种情况,您仍然需要在所有异步调用中使用Promise,以便Cloud Functions在完成所有工作后之后知道何时响应客户端.将return语句放在"A行"上,实际上是从get()开始的异步工作中返回promise,从而满足了这一要求.如果没有return语句,Cloud Functions将终止您的函数,因为它认为没有更多的工作可以发送最终响应.

In either case, you still need to make use of the promises in all your async calls so that Cloud Functions knows when to respond to the client, after all the work is complete. Placing the return statement on "line A" is effectively returning the promise from the async work started by get(), fulfilling this requirement. Without the return statement, Cloud Functions is terminated your function because it thinks there is no more work to complete in order to send the final response.

如果您不了解诺言如何在JavaScript中工作,请在此处观看我的视频教程:

If you're not familiar about how promises work in JavaScript, watch my video tutorials here: https://firebase.google.com/docs/functions/video-series/

这篇关于使用Firebase函数onCall在Firestore中获取CRUD数据.其中的任何异步操作(Promise)会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆