skip to Main Content

How do I pass this onRequest function to onCall? I am working from my localhost with emulators. Could someone give me some guidance, I have tried to follow the documentation of functions.https.onCall but I can’t understand if I have to do any previous step.

export const getFileInformation = functions.https.onRequest( (req, res) => {

  return cors( req, res, () => {

    const urls = [
      `url1`,
      `url2`,
      `url3`
    ];

    const urlsCalls: any[] = [];
    const resultados: any[] = [];

    urls.forEach( url => {
      urlsCalls.push(axios.get(url));
    });

    Promise.allSettled(urlsCalls)
    .then( response => {
      response.map( (element: any) => {
        const item = element.value.data;
        resultados.push(item);
      });
      console.log(resultados);
      res.json(resultados);
    })
    .catch( error => {
      console.log(error);
    });
  } );
});

I’m trying something as simple as this:

export const getFileInformation2 = functions.https.onCall( (data, context) => {
  return { msg: 'Hello from Firebase!' };
});

But I get the following error:

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

How should I address an onCall function?

2

Answers


  1. Chosen as BEST ANSWER

    Lo resolvĂ­ asi:

    index.ts

    export const getFileInformation2 = functions.https.onCall( (data, context) => {
    
        const urls = [
          `url1`,
          `url2`,
          `url3`
        ];
    
        const urlsCalls: any[] = [];
        const resultados: any[] = [];
    
        urls.forEach( url => {
          return urlsCalls.push(axios.get(url));
        });
    
        return Promise.allSettled(urlsCalls)
        .then( response => {
          response.map( (element: any) => {
            const item = element.value.data;
            resultados.push(item);
          });
          console.log(resultados);
          return resultados
        })
        .catch( error => {
          console.log(error);
        });
    
    });
    

    Luego ejecuto emulators:

    firebase emulators:start --only functions
    

    Luego lo llamo desde mi frontEnd:

    component.ts

    getFilesInformation() {
        const functions = getFunctions(getApp());
        connectFunctionsEmulator(functions, 'localhost', 5001);
    
        const getInfo = httpsCallable(functions, 'getFileInformation2')
        getInfo().then( (result: any) => {
          console.log(result.data);
        })
      }
    

  2. HTTPS Callable functions must be called using the POST method, the Content-Type must be application/json, and the body must contain a field called data for the data to be passed to the method.

    See sample body:

    {
        "data": {
            "someKey": "someValue"
        }
    }
    

    When pasting the URL directly to a browser it is called using the GET method which returns an error Request has invalid method. GET and the reason you’re getting {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}} is you’re not passing the required field which is data on your request body.

    You can call a https.onCall using curl or Postman.
    Sample call using curl:

    curl -d '{"data": {"someKey": "someValue"}}' -H 'Content-Type: application/json' http://localhost:5001/[myProjectName]/us-central1/getFileInformation2
    

    For more information, you may check :

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search