【问题标题】:Firebase cloud-function multiple executionsFirebase 云功能多次执行
【发布时间】:2018-06-15 13:51:41
【问题描述】:

我有一个如下的 firebase (Google) 云功能

// Initialize the Auth0 client
var AuthenticationClient = require('auth0').AuthenticationClient;
var auth0 = new AuthenticationClient({
    domain:       'familybank.auth0.com',
    clientID:     'REDACTED'
});

function getAccountBalance(app) {
    console.log('accessToken: ' + app.getUser().accessToken);
    auth0.getProfile(app.getUser().accessToken, function (err, userInfo) {
        if (err) {
            console.error('Error getting userProfile from Auth0: ' + err);
        }
        console.log('getAccountBalance userInfo:' + userInfo)

        let accountowner = app.getArgument(PARAM_ACCOUNT_OWNER);

        // query firestore based on user
        var transactions = db.collection('bank').doc(userInfo.email)
                          .db.collection('accounts').doc(accountowner)
                          .collection('transactions');
        var accountbalance = transactions.get()
            .then( snapshot => {
                var workingbalance = 0
                snapshot.forEach(doc => {
                    workingbalance = workingbalance + doc.data().amount;
                });

                app.tell(accountowner + " has a balance of $" + workingbalance)
            })
            .catch(err => {
                console.log('Error getting transactions', err);
                app.tell('I was unable to retrieve your balance at this time.')
            });
    });
}
actionMap.set(INTENT_ACCOUNT_BALANCE, getAccountBalance);
app.handleRequest(actionMap);

执行此操作时,我看到以下日志

请注意,函数的某些部分被多次执行,第二次执行失败。如果我在记录userInfo 后关闭auth0.getProfile 调用,则该函数有效,但显然没有userInfo

知道为什么这个函数的某些部分会执行多次以及为什么有些调用会失败吗?

【问题讨论】:

  • auth0的定义是什么,你用的是什么库? (看起来 auth0 调用了两次回调,至少来自您显示的日志片段。)
  • 我添加了创建 auth0 变量的代码。

标签: node.js firebase google-cloud-functions auth0


【解决方案1】:

userInfo 在点 (2) 处未定义,因为出现了错误(在其正下方的行上报告,这是之前记录的消息)。您的错误块不会离开函数,因此它会继续使用无效的 userInfo 对象运行。

但这并不能解释为什么回调会被调用两次——一次是有效的userInfo,一次是errAuthenticationClient.getProfile() 的文档(尽管不是示例)表明它返回一个 Promise(或 undefined - 尽管它没有说明为什么它可能返回 undefined),所以我想知道这是否最终会调用回调两次。

因为它返回一个承诺,你可以省略回调函数,只用这样的方式处理它:

function getAccountBalance(app) {
    let accountowner = app.getArgument(PARAM_ACCOUNT_OWNER);
    console.log('accessToken: ' + app.getUser().accessToken);
    var accessToken = app.getUser().accessToken;
    auth0.getProfile( accessToken )

      .then( userInfo => {
        console.log('getAccountBalance userInfo:' + userInfo)

        // query firestore based on user
        var transactions = db.collection('bank').doc(userInfo.email)
                          .db.collection('accounts').doc(accountowner)
                          .collection('transactions');
        return transactions.get();
      })

      .then( snapshot => {
        var workingbalance = 0
        snapshot.forEach(doc => {
          workingbalance = workingbalance + doc.data().amount;
        });

        app.tell(accountowner + " has a balance of $" + workingbalance)
      })

      .catch( err => {
        console.error('Error:', err );
        app.tell('I was unable to retrieve your balance at this time.')
      })

    });
}

【讨论】:

  • 我最终得到了一些非常相似的东西,它确实有效。我从未发现双重通话的根本原因。
猜你喜欢
  • 2019-12-23
  • 2019-06-29
  • 2021-10-26
  • 2018-11-07
  • 2019-07-14
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多