【发布时间】:2020-03-22 20:50:29
【问题描述】:
我必须调用函数:getMatchDataApi() 和 saveApiDataToDb()。 getMatchDataApi() 函数从 api 返回值,saveApiDataToDb() 函数用于将 getMatchDataApi() 值存储到 firestore 数据库中。
function getMatchDataApi() {
var options = {
method: "GET",
hostname: "dev132-cricket-live-scores-v1.p.rapidapi.com",
port: null,
path: "/scorecards.php?seriesid=2141&matchid=43431",
headers: {
"x-rapidapi-host": "dev132-cricket-live-scores-v1.p.rapidapi.com",
"x-rapidapi-key": "63e55e4f7fmsh8711fb1c0bd9ec2p1d8b4bjsne2b8db0a1a82"
},
json: true
};
var req = http.request(options, res => {
var chunks = [];
res.on("data", chunk => {
chunks.push(chunk);
});
res.on("end", () => {
var body = Buffer.concat(chunks);
var json = JSON.parse(body);
playerName = json.fullScorecardAwards.manOfTheMatchName;
console.log("player name", playerName);
});
});
req.end();
}
async function saveApiDataToDb() {
await getMatchDataApi();
var name = playerName;
console.log("Aman Singh", name);
}
我在这里使用异步功能。所以首先我希望它应该首先执行这个 getMatchDataApi() 并返回值,然后它应该在这个函数 saveApiDataToDb() 中打印值。 然后我调用 saveApiDataToDb() 如下:
exports.storeMatchData = functions.https.onRequest((request, response) => {
saveApiDataToDb()
});
【问题讨论】:
-
我是 Firebase 中云功能的新手。请帮助任何人。提前致谢
-
getMatchDataApi没有返回任何承诺,所以你不能等待它。另外,playerName是全局变量吗? -
你的函数应该返回一个承诺,当所有异步工作完成时解决。 firebase.google.com/docs/functions/terminate-functions
标签: javascript node.js firebase google-cloud-functions