how to make get request firebase cloud functions

眉间皱痕 提交于 2020-01-16 18:45:28

问题


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

I am getting this error

Function returned undefined, expected Promise or value

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})
      });
});

sorry about my english


回答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
    };

    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;
      });
  });


来源:https://stackoverflow.com/questions/56410014/how-to-make-get-request-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!