使用格子从Firebase Cloud可调用函数返回数据

时间:2019-07-17 03:40:51

标签: firebase google-cloud-functions plaid

您好,我正在调用firebase可调用函数,但它始终返回null。我不确定我到底在做什么错。我正在尝试使用格子来交换我的令牌并获取访问令牌,但是这里是我使用https://plaid.com/docs/https://firebase.google.com/docs/functions/callable作为编写代码的引用。任何建议将不胜感激

exchange token 

const exchange_token = (data, context) => {

    const public_token = data.public_token;

    if (!context.auth) {
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
            'while authenticated.');
    }
    return Plaid.client.exchangePublicToken(public_token, (error, tokenResponse) => {
        if (error !== null) {
            var msg = 'Could not exchange public_token!';
            return {
                status: 400,
                error: msg
            }
        }
        ACCESS_TOKEN = tokenResponse.access_token;
        ITEM_ID = tokenResponse.item_id;
        console.log("Access token: " + ACCESS_TOKEN + " Item Id: " + ITEM_ID);
        return {
            access_token: ACCESS_TOKEN,
            item_id: ITEM_ID,
            error: false
        };
    });
}

and my front end service 

exchangeToken(public_token: string){
    const exchangeToken$ = this.fireFunctions.httpsCallable("exchangeToken");
    return exchangeToken$({public_token: public_token});
  }
  
  and then my component 
  
   this.bankService.exchangeToken(event.token).subscribe(
      value => this.processToken(value),
      error => this.handleError(),
      () => this.finished = true)

1 个答案:

答案 0 :(得分:0)

你好,我知道了,解决方案是将格子请求包装在一个承诺中,然后像这样返回该承诺

function exhangeToken(resolve, reject ){
    Plaid.client.exchangePublicToken(public_token, (error, tokenResponse) => {
       if (error !== null) {
            var msg = 'Could not exchange public_token!';
            reject( {
                status: 400,
                message: msg
            })
        }
        ACCESS_TOKEN = tokenResponse.access_token;
        ITEM_ID = tokenResponse.item_id;
        console.log("Access token: " + ACCESS_TOKEN + " Item Id: " + ITEM_ID);
        resolve( {
            status: 200,
            message: "Token exchange success"
        });
    });
}
return new Promise(exhangeToken);