1

I have a simple firebase functions script setup (running firebase-admin version 8.0 and firebase-functions version 2.3.1):

const functions = require('firebase-functions');
const cors = require('cors')({
    origin: true,
});

//Gets and returns a user's ip address
exports.getIPAddress = functions.https.onRequest((req, res) => {
    let ipAddress = req.headers['fastly-client-ip'] || req.connection.remoteAddress;
    ipAddress = ipAddress.toString();
    console.log('Fetched IP Address: ' + ipAddress);
    return cors(req, res, () => {
        res.status(200).send(ipAddress);
    });
});

The function's goal is simply to return to user's IP address. It logs fine in the functions console, no errors.

Here is the client code:

var getIPAddress = mainFirebase.functions().httpsCallable('getIPAddress');
function testIP() {
    getIPAddress().then(function(result) {
        console.log(result.data.text)
    });
}

However, the console says that 'result' is not a valid JSON object.

I've tried using https.onCall which somebody else on the internet recommended, however, the console says that function doesn't exist.

Any help getting the response to work properly would be greatly appreciated!

TheRyan722
  • 1,029
  • 13
  • 37

2 Answers2

1

Your function is a regular HTTP type function. However, your client code is attempting to call it as if it were a callable type function. That's not going to work. If you want to invoke a callable type function, you'll have to implement the function according to the documentation.

If you need to keep the function as an HTTP type function, you can't use the Firebase client SDK to invoke it. Just invoke it as if it were any other type of HTTP endpoint.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I've tried that as well, but then the console (client side) gives me a POST error (even though I'm not posting any data). – TheRyan722 Jun 11 '19 at 17:36
  • Nevermind, I seem to be on track to figuring out what the issue was. Will report back when I finalize it. – TheRyan722 Jun 11 '19 at 18:03
  • It was a server issue because in onCall() the request headers are in context.rawRequest. I really wish this was documented/a documented difference. – TheRyan722 Jun 11 '19 at 18:08
1

For Callable functions. You need to create a function like:

exports.addMessage = functions.https.onCall(
  async (data, context) => {
    // context contains the user info.
  }
);

And on your front-end you can call them like:

firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
}).catch(function(error) {
  // Getting the Error details.
  var code = error.code;
  var message = error.message;
  var details = error.details;
  // ...
});

As you are calling an https message. You can also use the SDK to call https methods. But make sure you are handling CORS on your server.

In your client. Just use the http client.

this.http.post method with the function url.

Muhammad Kamran
  • 978
  • 6
  • 10