javascript - Firebase 云函数超时

标签 javascript firebase google-cloud-functions google-cloud-firestore

以下函数在用 shell 测试时运行良好,数据是在 firestore 中创建的。 当推送到产品中时,它返回 Function execution take 60002 ms, finished with status: 'timeout'

有什么意见吗?

exports.synchronizeAzavista = functions.auth.user().onCreate(event => {

    console.log('New User Created');

    const user = event.data;
    const email = user.email;
    const uid = user.uid;

    return admin.database().ref(`/delegates`)
        .orderByChild(`email`)
        .equalTo(email)
        .once("child_added").then(snap => {
            const fbUserRef = snap.key;

            return admin.firestore().collection(`/users`).doc(`${fbUserRef}`).set({
                email: email,
                uid: uid
            }).then(() => console.log("User Created"));
        });
});

编辑

我已经用以下内容更新了我的代码,但我仍然得到 Function returned undefined, expected Promise or value 但我无法确定我的函数在哪里返回未定义。为什么我的 getUser() 函数没有返回任何内容?

const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.synchronizeAzavista = functions.auth.user().onCreate(event => {

    console.log('New User Created');//This log

    const user = event.data;
    const email = user.email;
    const uid = user.uid;

    console.log('Const are set');//This log

    getUser(email).then(snap => {

        console.log("User Key is " + snap.key);//No log

        const fbUserRef = snap.key;

        return admin.firestore().collection(`/users`).doc(`${fbUserRef}`).set({
            email: email,
            uid: uid
        });
    }).then(() => console.log("User Data transferred in Firestore"));
});

function getUser(email) {

    console.log("Start GetUser for " + email);//This log 

    const snapKey = admin.database().ref(`/delegates`).orderByChild(`email`).equalTo(email).once("child_added").then(snap => {
        console.log(snap.key);//No Log here
return snap;
    });
    return snapKey;
}

最佳答案

您不会通过写入 Firestore 来返回 promise 。

exports.synchronizeAzavista = functions.auth.user().onCreate(event => {
    const user = event.data;
    const email = user.email;
    const uid = user.uid;

    return admin.database().ref(`/delegates`)
        .orderByChild(`email`)
        .equalTo(email)
        .once("child_added").then(snap => {
            const fbUserRef = snap.key;

            return admin.firestore().collection(`/users`).doc(`${fbUserRef}`).set({
                email: email,
                uid: uid
            });
        });
});

关于javascript - Firebase 云函数超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500121/

相关文章:

javascript - 需要更深入地了解 json,但在 ajax 服务器响应时我无法做到这一点

javascript - 我怎样才能让这个JS slider 每三秒自动播放一次

javascript - Ruby on Rails - 将 JavaScript 变量从 Controller 发送到外部 Javascript Assets 文件

javascript - 循环条件的缓存数组长度会影响性能吗?

Java Firebase 错误 "NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier"

swift - 复杂的 firebase 读取查询 - String、Float、Int 和 NSArray

firebase - 跨所有选项卡获取 Firebase session

ios - Firebase - 如何获取 observeEventType = Value 中的键值

javascript - 尝试将生成的PDF发送到谷歌云功能以使用nodemailer发送电子邮件

Firebase功能-您已超出部署配额