【发布时间】: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