How To Make Get Request Firebase Cloud Functions
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 ma
Solution 1:
As explained by Doug in his comments above, you need to return a Promise.
The request
library you are using supports callback interfaces natively but does not return a promise.
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
};
returnrp(options)
.then(parsedBody => {
return admin
.firestore()
.doc('geocoder/' + context.params.location)
.update({ city: parsedBody.address.state });
})
.catch(err => {
console.log(err);
returnnull;
});
});
Post a Comment for "How To Make Get Request Firebase Cloud Functions"