Cloud Functions for Firebase onWrite 超时 [英] Cloud Functions for Firebase onWrite timeout

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

问题描述

我返回交易承诺,它应该在停止功能之前等待交易完成.交易执行良好,但承诺似乎永远无法解决.

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.

我在 Firebase 控制台中看到此函数总是在 60 秒后超时.

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;
});

我用下面的hack"解决了这个问题,我自己创建了一个承诺,因为无论 .transaction 返回什么都不起作用:

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));
});

推荐答案

旧版本的 firebase-admin SDK 存在一个已知问题,即 Firebase 数据库引用和快照无法进行 JSON 序列化,因此无法使用作为 Cloud Functions 的返回值.这包括交易返回值,因为它们也有快照.

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.

您的 hack 可以解决该错误;如果你更新你的 firebase-admin 版本,你也应该得到修复.

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

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

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