Firebase云功能与批量更新不一致 [英] Firebase cloud functions inconsistent with bulk updates

查看:83
本文介绍了Firebase云功能与批量更新不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的firebase云功能,我在其中调用这样的外部api端点.

I have my firebase cloud function in which I am calling my external api end point like this.

const functions = require('firebase-functions');
var admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
var request = require('request');
var moment = require('moment');
var rp = require('request-promise');

var db = admin.database();

exports.onCheckIn = functions.database.ref('/news/{newsId}/{entryId}')
      .onCreate(event => {
        console.log("Event Triggered");
        var eventSnapshot = event.data.val();
        request.post({
              url: 'http://MyCustomURL/endpoint/',
              form: {
                data: eventSnapshot.data
             }
            }, function(error, response, body){

                console.log(response);

            });



      })

我正在使用Blaze计划,这工作正常.但是问题是,当我创建批量数据(大约50至100个条目)时,指向我的自定义网址的HTTP请求无法正常工作.一个或两个HTTP请求被跳过了.

I am using Blaze plan and this is working completely fine. But the problem is that when I am creating bulk data (around 50 to 100 entries) the HTTP request at to my custom url is not working properly.One or two HTTP request are being skipped.

我已经调试了自定义服务器,发现该服务器未收到来自Firebase的丢失请求.但是我也检查了云功能日志,发现每个事件都被正确触发了.

I have debugged my custom server and found out that it is not receiving missing requests from firebase. But I have also checked the cloud function logs and I can find that every event is correctly being triggered.

可能是什么问题?我做错什么了吗?

What could be the problem? Am I doing anything wrong ?

推荐答案

您不会从函数中返回任何值.这意味着Cloud Functions假定该函数在最后一条语句运行之后即已完成其工作.但是,由于您要在函数中进行异步HTTP调用,因此该调用可能尚未完成.不幸的是,您没有告诉Cloud Functions您有一个未完成的调用,因此它可能在您返回后的任何时间终止您的函数.

You're not returning any value from your function. This means that Cloud Functions assumes that the function is done with its work after the last statement has run. But since you're making an asynchronous HTTP call in the function, that calls may not have completed yet. Unfortunately you're not telling Cloud Functions about the fact that you have an outstanding call, so it may kill your function at any time after you return.

解决方案是返回一个在HTTP请求完成后解析的Promise.由于您已经包含了request-promise,因此很简单:

The solution is to return a promise that resolves after the HTTP request has completed. Since you're already including request-promise this is simple:

exports.onCheckIn = functions.database.ref('/news/{newsId}/{entryId}')
.onCreate(event => {
  console.log("Event Triggered");
  var eventSnapshot = event.data.val();
  return rp.post({
      url: 'http://MyCustomURL/endpoint/',
      form: {
        data: eventSnapshot.data
      }
  });
})

这是不熟悉JavaScript或JavaScript和Firebase的开发人员的常见问题,内容涵盖于:

This is a common problem for developers new to JavaScript, or with JavaScript and Firebase, and is covered in:

  • the Firebase documentation
  • this blog post
  • this video

这篇关于Firebase云功能与批量更新不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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