Возврат данных Axios из модуля Firebase - PullRequest
0 голосов
/ 21 марта 2019

Это в моем index.js:

const dt = require('./src/dash-token');
exports.getToken = functions.https.onRequest((req, res) => {
    res.send("res : "+dt.getDashToken()) ;
});

Это в моем файле dash-token.js:

const axios = require("axios");
const urlokta = 'xxx.com/v1/token?client_id=xxx';

exports.getDashToken = function () {
  console.log("getDashToken called !")
  return ax().then(data => {return data});             
}

async function ax() {
  console.log("ax called..")
  try{
    //const axx =  await axios.post(urlokta)
    const axx =  await axios.post(urlokta).then(data=>{return data})
    console.log(axx.data.access_token);
    return axx;
  }
    catch (error) {
      console.log(error);
    };
};

Это в моей консоли Firebase:

2:15:31.332 PM getToken xxx
2:15:29.551 PM getToken Function execution took 6 ms, finished with status code: 200
2:15:29.549 PM getToken ax called..
2:15:29.548 PM getToken getDashToken called -
2:15:29.545 PM getToken Function execution started

Это возвращает обещание, которое я не могу понять, как решить.Можете ли вы сказать мне, что я не делаю правильно?

1 Ответ

0 голосов
/ 21 марта 2019

Из того, что вы описываете в своем вопросе, вы должны сделать следующее, чтобы обработать возвращенное обещание от getDashToken, если я не ошибаюсь:

const dt = require('./src/dash-token');
exports.getToken = functions.https.onRequest((req, res) => {
    dt.getDashToken()
    .then(data => {
        res.send("res : " + data) ;
    })
 });
...