0
votes

Hello I am calling a firebase callable function, but it always returns null. I am not sure what exactly I am doing wrong. I am trying to use plaid to exchange my token and get the access token, but here are the references i used https://plaid.com/docs/ and https://firebase.google.com/docs/functions/callable as references to write my code. Any suggestions would be really appreciated

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
Sounds like it's time to add some debug logging to figure out what exactly is not working the way you expect. - Doug Stevenson
@DougStevenson I can see that the ACCESS_TOKEN is there in that function and i my console log prints it out. But somehow it does not get returned. I'm i returning this and consuming it in the right way? - inhaler
It looks like the plaid exchange token function returns void. @DougStevenson do you have ansy suggestions on how to handle a situation like this. I would need to return something to my client - inhaler

1 Answers

0
votes

Hello friends i figured it out, the solution is to wrap the plaid request in a promise and return that promise like so

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