Firebase onWrite超时的云端函数 [英] Cloud Functions for Firebase onWrite timeout

查看:176
本文介绍了Firebase onWrite超时的云端函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还要在交易完成之前等待交易完成停止功能。该交易执行正常,但承诺似乎永远不会解决。



我在Firebase控制台中看到此功能在60秒后总是超时。

  const functions = require('firebase-functions'); 
const admin = require(firebase-admin);
const db = admin.database();

$ b导出let countFollowers = functions.database.ref('followers / {followee} / {follower}')。onWrite(event => {
const followee = event .params.followee;
let path =`posts / $ {followee} / cnt_foll`;
const countRef = db.ref(path);
let out = countRef.transaction(current = > {
if(event.data.exists()&&!event.data.previous.exists()){
return(parseInt(current)|| 0)+ 1;
} else if(!event.data.exists()&& event.data.previous.exists()){
return(parseInt(current)|| 0) - 1;
}
});

退货;
});

编辑:

我解决了这个问题用下面的黑客,我自己创建一个承诺,因为无论 .transaction 返回的是不工作的:



<$ ()函数返回新的Promise(函数(解析,拒绝){
countRef.transaction(current => {
if(event.data.exists()&& amp; ;!event.data.previous.exists()){
return(parseInt(current)|| 0)+ 1;
} else if(!event.data.exists()&& event.data.previous.exists()){
return(parseInt(current)|| 0) - 1;
}
},()=> resolve(null));
});


解决方案

老版本的firebase存在一个已知问题-admin SDK,其中Firebase数据库引用和快照不能被JSON序列化,因此无法用于Cloud Functions的返回值。这包括事务返回值,因为它们也有快照。



如果你更新了你的firebase-admin版本,你也应该得到修正。


I return transaction promise which should wait for transaction to finish before stopping the function. The transaction executes fine, but the promise seems to never resolve.

I see in the Firebase console that this function always times out after 60s.

const functions = require('firebase-functions');
const admin = require("firebase-admin");
const db = admin.database();


export let countFollowers = functions.database.ref('followers/{followee}/{follower}').onWrite(event => {
    const followee = event.params.followee;
    let path = `posts/${followee}/cnt_foll`;
    const countRef = db.ref(path);
    let out = countRef.transaction(current => {
        if (event.data.exists() && !event.data.previous.exists()) {
            return (parseInt(current) || 0) + 1;
        } else if (!event.data.exists() && event.data.previous.exists()) {
            return (parseInt(current) || 0) - 1;
        }
    });

    return out;
});

EDIT:

I solve the problem with the following "hack", I create a promise myself, because whatever .transaction is returning is not working:

return new Promise(function(resolve, reject) {
    countRef.transaction(current => {
        if (event.data.exists() && !event.data.previous.exists()) {
            return (parseInt(current) || 0) + 1;
        } else if (!event.data.exists() && event.data.previous.exists()) {
            return (parseInt(current) || 0) - 1; 
        }
    }, () => resolve(null));
});

解决方案

There was a known issue with older versions of the firebase-admin SDK where Firebase Database references and snapshots couldn't be JSON serialized and thus couldn't be used in return values for Cloud Functions. This includes transaction return values since they also have snapshots.

Your hack works around the bug; you should also get the fix if you update your version of firebase-admin.

这篇关于Firebase onWrite超时的云端函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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