我有一个应用程序,我想每 5 分钟更新一次天气详细信息。我在 Google 云函数上构建了一个PubSub 函数,以从weatherstack.com网站获取天气详细信息,然后我尝试使用Admin SDK将结果保存在 FireStore 上。天气 API 工作正常,但未触发将数据写入 FireStore 的功能。
我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const requestApi = require('request');
admin.initializeApp();
exports.WeatherPubSub = (event, context) => {
requestApi({
url: 'http://api.weatherstack.com/current?access_key=XXXXXXXXXXXXXXXXXXXX&query=XXXXX',
method: "POST"
}, (error, response, body) => {
//console.log(JSON.parse(body));
if (error) {
console.log('error:', error); // Print the error if one occurred
} else {
console.log("=> "+ response.statusCode);
//Call firestore to update weather data
return admin.firestore().collection('settings').doc('weather').update({
temperature: body.current['temperature'],
humidity: body.current['humidity'],
weather_descriptions: body.current['weather_descriptions'][0]
}).then(function() {
console.log('Done!');
});
}
});
//return null;
};
包.json
{
"name": "functions",
"version": "0.0.2",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"@google-cloud/pubsub": "^1.5.0",
"firebase-admin": "^8.9.2",
"firebase-functions": "^3.3.0",
"request": "^2.88.2"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}
您必须返回一个承诺,当您的 Cloud Function 中的所有异步工作完成时(即调用weatherstack
API 和 Firestoreupdate()
方法)。
此处的Firebase 官方视频系列对此进行了解释。特别是观看标题为“Learn JavaScript Promises”的三个视频(第 2 部分和第 3 部分特别关注后台触发的云函数,但之前的第 1 部分确实值得一看)。
request
您使用的库本身支持回调接口,但不返回 promise。
您可以使用request-promise
库和rp()
“返回常规 Promises/A+ 兼容承诺”的方法,然后按如下方式调整您的代码:
//.....
var options = {
method: 'POST',
uri: 'http://api.weatherstack.com/current?access_key=XXXXXXXXXXXXXXXXXXXX&query=XXXXX'
};
return rp(options)
.then(body => {
return admin.firestore().collection('settings').doc('weather').update({
temperature: body.current['temperature'],
humidity: body.current['humidity'],
weather_descriptions: body.current['weather_descriptions'][0]
}
})
.catch(err => {
console.log(err);
return null;
});
//.....
此外,您应该仔细检查您声明 cloud Function ( exports.WeatherPubSub = (event, context) => {...}
) 的方式是否正确。似乎并非如此,有关更多详细信息,请参阅文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句