如何使用Firebase函数从外部URL(PLACES API)抓取JSON文件 [英] How to grab json file from external url(PLACES API) using firebase functions
问题描述
所以, 我想使用云功能将请求从我的应用程序发送到Firebase,然后处理进程url并从Places api发送回JSON文件
So, I want to send a request from my app to Firebase using cloud funtions and then process process url and send back JSON file from places api
我已经完成/已经完成的事情=> 在控制台中设置项目并获取Firebase CLI后,创建了一个云功能,如下所示
WHAT I HAVE ALREADY DONE/HAVE=> After Setting up the project in console and getting firebase CLI created a cloud function as follows
关注您的评论后 这是我的全部功能代码:
After following your comments this is my full function code:
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类中传递了这样的值
and in java class passed values like this
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
作为错误
Now What my problem is while deploying new function code with firebase CLI I am getting 16:8 error Each then() should return a value or throw promise/always-return
as error
任何人都可以引导我..
Can anyone guide me please..,
URL将如下所示:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4369681,78.4473887&radius=5000&type=airport&sensor=true&key=MY_KEY
URL will be like this :
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4369681,78.4473887&radius=5000&type=airport&sensor=true&key=MY_KEY
这是控制台中的日志详细信息
Here are the log details from console
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)
推荐答案
这对我有用.在promise返回promise的结果之前使用return,并且在使用functions.https.onCall
和res.send
时我们需要使用return
.使用functions.https.onRequest
时,我花了3天的时间来获得&当您的网址返回json时,无需在请求承诺选项中添加json=true
,这只会使事情变得复杂
This worked for me., using return before promise returns the result from promise, and we need to use return
when using functions.https.onCall
and res.send
when using functions.https.onRequest
it took me 3 days to get it & no need of adding json=true
in request-promise options when your url returns a json, it just made things complicated
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;
});
})
这篇关于如何使用Firebase函数从外部URL(PLACES API)抓取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!