简体   繁体   中英

Firebase functions not working with axios

const functions = require('firebase-functions')
const axios = require('axios')

exports.getTown = functions.https.onCall((data, context) => {

    axios.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${data.lat},${data.lng}&result_type=locality&key=**********`)
    .then(town => {
        return town
    }).catch(err => {
        return err
    })


})

When I call this in the front end I just get an error in the console:

POST https://europe-west2-heretic-hearts.cloudfunctions.net/getTown 500
Uncaught (in promise) Error: INTERNAL

I've tested to make sure the incoming data is being received properly and it is, so the problem must be in the function itself. But I can't see what could possibly be going wrong here...?

You can't invoke an onCall type function with a simple POST request. Callable functions have a specific protocol that they use on top of HTTP. If you can't reproduce that protocol, the function will fail every time.

If you want to write a simple HTTP function endpoint, then follow the instruction for writing an HTTP trigger instead using onRequest. It works very differently.

Also, I'm noticing that you're not handling promises correctly in your function. Please read the documentation thoroughly to understand what you need to do with promises in order to get your function to execute correctly, no matter what type of function you write.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM