如何使获取请求的Firebase云功能 [英] how to make get request firebase cloud functions
问题描述
我正在开发一个应用程序,我想将用户坐标从firestore发送到locationiq.com以获取详细信息,然后将其写入firestore中,我正在使用firebase云功能来进行GET请求,但我认为它失败了有人可以帮助我提供代码
I am developing an app and I want to send my user coordinate from firestore to locationiq.com to get details and then write it in firestore I am using firebase cloud function to make GET request but I think it fails can someone help me with the code
我收到此错误
函数返回了未定义的预期承诺或值
const functions = require('firebase-functions');
const request = require('request');
const admin = require ('firebase-admin');
admin.initializeApp();
// Create and Deploy Your First Cloud Functions.
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.geoCoder = functions.firestore
.document('geocoder/{location}').onCreate((snap, context) => {
const latitude = snap.data().coordinate.latitude;
const longtude = snap.data().coordinate.longtude;
var part1 = "https://us1.locationiq.com/v1/reverse.php?key=XXXXXXXXXXXXXX&lat=";
var part2 = "&lon="
var part3 = "&format=json"
request(part1 + latitude + part2 + longtude + part3, { json: true }, (err, res, body) => {
if (err) {
console.log(err);
}
console.log(body.address);
});
});
更新,我遇到相同的错误.
UPDATE I am facing the same error.
const functions = require('firebase-functions');
const request = require('request');
const admin = require ('firebase-admin');
admin.initializeApp();
exports.geoCoder = functions.firestore.document('geocoder/{location}').onCreate((snap, context) => {
const latitude = snap.data().coordinate.latitude;
const longtude = snap.data().coordinate.longtude;
var part1 = "https://us1.locationiq.com/v1/reverse.php?key=XXXXXXXXXXXXXX&lat=";
var part2 = "&lon="
var part3 = "&format=json"
request(part1 + latitude + part2 + longtude + part3, { json: true }, (err, res, body) => {
if (err) {
console.log(err);
return 0;
}
return admin.firestore().doc("geocoder/" + context.params.location).update({city: body.address.state})
});
});
对不起我的英语
推荐答案
如道格在上面的评论中所述,您需要返回一个Promise.
As explained by Doug in his comments above, you need to return a Promise.
您正在使用的 request
库本身支持回调接口,但不返回承诺.
The request
library you are using supports callback interfaces natively but does not return a promise.
您可以使用 request-promise
( https://github.com/request/request-promise )和 rp()
方法,该方法返回符合Promises/A +的常规承诺",然后按如下所示修改您的代码:
You can use request-promise
(https://github.com/request/request-promise) and the rp()
method which "returns a regular Promises/A+ compliant promise" and then adapt your code as follows:
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
const rp = require('request-promise');
admin.initializeApp();
exports.geoCoder = functions.firestore
.document('geocoder/{location}')
.onCreate((snap, context) => {
const latitude = snap.data().coordinate.latitude;
const longtude = snap.data().coordinate.longtude;
const part1 =
'https://us1.locationiq.com/v1/reverse.php?key=XXXXXXXXXXXXXX&lat=';
const part2 = '&lon=';
const part3 = '&format=json';
const options = {
uri: part1 + latitude + part2 + longtude + part3,
json: true // Automatically parses the JSON string in the response
};
return rp(options)
.then(parsedBody => {
return admin
.firestore()
.doc('geocoder/' + context.params.location)
.update({ city: parsedBody.address.state });
})
.catch(err => {
console.log(err);
return null;
});
});
这篇关于如何使获取请求的Firebase云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!