【问题标题】:How to grab json file from external url(PLACES API) using firebase functions如何使用 firebase 函数从外部 url(PLACES API)获取 json 文件
【发布时间】:2019-11-14 00:04:01
【问题描述】:

所以, 我想使用云功能从我的应用向 Firebase 发送请求,然后处理进程 url 并从位置 api 发回 JSON 文件

我已经完成/拥有的=> 在控制台中设置项目并获取 firebase CLI 后创建了一个云功能,如下所示

在关注你的 cmets 之后 这是我的完整功能代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const rp = require('request-promise');

exports.fetch = functions.https.onCall((req, res) => {

    const url = req.url + '&key=MY_API_KEY';
    var options = {
        uri: url, // Automatically parses the JSON string in the response
        json: true
    };  

    rp(options)
    .then(result => {
        console.log('Get response:' + response.statusCode);
        return res.type('application/json').send(result);
    }).catch(err => {
        // API call failed...
        return res.send({'Error': err});
    });

})

在java类中传递这样的值

     private Task<String> addMessage(String url) {
            // Create the arguments to the callable function.
            Map<String, Object> data = new HashMap<>();
            data.put("url", url);///PASSING VALUES HERE
            return mFunctions
                    .getHttpsCallable("fetch")
                    .call(data)
                    .continueWith(task -> 
(String) Objects.requireNonNull(task.getResult()).getData());
        }

现在我在使用 firebase CLI 部署新功能代码时遇到的问题是 16:8 error Each then() should return a value or throw promise/always-return 错误

谁能指导我..,

网址将是这样的: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4369681,78.4473887&amp;radius=5000&amp;type=airport&amp;sensor=true&amp;key=MY_KEY

这是来自控制台的日志详细信息

2019-07-05T10:06:35.025308453Z D fetch: Function execution started
2019-07-05T10:06:35.265840608Z D fetch: Function execution took 241 ms, finished with status code: 200
2019-07-05T10:06:45.162Z I fetch: Get response:undefined
2019-07-05T10:06:46.062Z E fetch: Unhandled rejection
2019-07-05T10:06:46.062Z E fetch: TypeError: res.send is not a function
    at rp.then.catch.err (/srv/index.js:22:14)
    at bound (domain.js:301:14)
    at runBound (domain.js:314:12)
    at tryCatcher (/srv/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/srv/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/srv/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/srv/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/srv/node_modules/bluebird/js/release/promise.js:695:18)
    at _drainQueueStep (/srv/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/srv/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/srv/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues (/srv/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)

【问题讨论】:

  • 如果您有解决方案,您应该将其添加为答案,而不是将其放在您的问题中。
  • 哦...,对不起,我会添加它作为答案然后

标签: android json firebase google-cloud-functions google-places-api


【解决方案1】:

这对我有用。,在promise返回promise的结果之前使用return,我们需要在使用functions.https.onCallres.send时使用return,当使用functions.https.onRequest时我花了3天时间才得到它&当你的 url 返回一个 json 时,不需要在 request-promise 选项中添加json=true,它只会让事情变得复杂

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const rp = require('request-promise');

exports.fetch = functions.https.onCall((req, res) => {

    const url = req.url + '&key=MY_API_KEY';
    var options = {
        uri: url, // Automatically parses the JSON string in the response
    };

    return rp(options)
    .then(result => {
        console.log('here is response: ' + result);
        return result;
    }).catch(err => {
        // API call failed...
        return err;
    });

})

【讨论】:

【解决方案2】:

您应该在 Cloud Function 中使用 Promise 来处理异步任务(例如对 URL 的调用)。默认情况下 request 不返回 Promise,因此您需要为请求使用接口包装器,例如 request-promise

以下改编应该可以解决问题:

const rp = require('request-promise');

exports.fetch = functions.https.onCall((req, res) => {

  const url = req.url + '&key=MY_API_KEY';
  console.log(url);

  var options = {
    uri: url,
    json: true // Automatically parses the JSON string in the response
  };

  rp(options)
    .then(response => {
      console.log('Get response: ' + response.statusCode);
      res.send(response);
    })
    .catch(err => {
      // API call failed...
      res.status(500).send('Error': err);
    });

})

【讨论】:

  • 嗨,感谢您的回答,但是当我尝试使用您的代码部署函数时,它给出了 non zero exit code 1 所以我用 return 替换了 res.send 然后它部署了,现在我需要测试它是否作品
  • 再次返回空值
  • 您必须使用 res.send 完成您的 HTTP 云函数。您可以观看以下官方视频:youtube.com/watch?v=7IkUgCLr5oA。您应该通过添加函数的整个代码来修改您的问题,以便我们尝试您的代码并重现错误。
  • 好的,我已经用 res.send 再次替换它,现在我收到此错误error Each then() should return a value or throw promise/always-return
  • 更新代码完整功能代码请帮帮我
猜你喜欢
  • 1970-01-01
  • 2013-05-06
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2013-11-03
相关资源
最近更新 更多