Chris126
Chris126

Reputation: 35

Firebase Functions iOS Returns Error Not Found

I have built and deployed the default 'Hello World' firebase functions, however when I try and call it the error message is 'NOT FOUND'. Any help would be greatly appreciated. Thanks

Swift Code

func cloudRequest(){
    functions.httpsCallable("testFunction").call("") {(result, error) in
        if let error = error as? NSError{
            switch FunctionsErrorCode(rawValue: error.code) {
            case .internal:
                print("Internal error \(error.localizedDescription)")
            default:
                print("Error \(error.localizedDescription)")
            }
        }
        print("Running result condition")
        if error == nil{
            print(result?.data)
        }
    }
}

The code for the function deployed in GCP eu west 2 is

exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Upvotes: 1

Views: 1850

Answers (2)

Ajordat
Ajordat

Reputation: 1392

As per the updated code, it seems that the target Firebase Function is called helloWorld, not testFunction. Moreover, it is located in region europe-west2. Actually, it also seems like you are using Cloud Functions instead of Firebase Functions.

Notice that a Firebase project has a GCP counterpart, therefore a Firebase Function appears in both projects but a GCP project does not necessarily have a Firebase project linked to it. You will find more information on this in the documentation.

If you intend to call the Cloud Function from iOS, I suggest you move to Firebase Functions, where you will be able to use the Firebase SDK to trigger the function (what you intended to do).

Firebase library

Use these instructions to learn how to properly write a Firebase Functions and how to deploy it. You should have something like this:

'use strict';

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

exports.helloWorld = functions.region('europe-west2').https.onRequest((req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
});

Notice that the region is specified when creating the function, otherwise it would be created in us-central1.

After that, it will be possible to reach the function using Firebase from you iOS app.

lazy var functions = Functions.functions(region:"europe-west2")

functions.httpsCallable("helloWorld").call() { (result, error) in
    // code
}

This method forces you to deploy a Firebase Function on a Firebase project, it does not work on a GCP-only project.

HTTP Trigger

If you intend to keep using GCP, you can access your deployed functions using their HTTP trigger. In this case it would be something like the following but with the correct project ID:

https://europe-west2-PROJECT_ID.cloudfunctions.net/helloWorld

This method also works using a Firebase Function.

Upvotes: 1

Muthu Thavamani
Muthu Thavamani

Reputation: 1405

  1. check whether the function name is correct

  2. To call a function running in any location other than the default us-central1, you must set the appropriate value at initialization. For example, on Android you would initialize with getInstance(FirebaseApp app, String region).

Ex, lets assume europe-west3 where your function deployed; to call this function

 functions('europe-west3').httpsCallable('testFunction')(/* ... */)

Useful ref:

Upvotes: 2

Related Questions