Calling a netlify function inside another netlify function

Hello!

I am a third year CS student who have been testing out netlify and faunadb during my christmas vacation. I have created two separate netlify functions, one getting price information from a crypto api, and another that gets the user data from faunadb.

My site is not in production, so only testing in dev-mode.

Previously it was the client who managed and merged the different data sources together, but i wanted to try to move this logic to one single endpoint/function, thus i just cut out the block of code, calling the price-api, from my react component, to my “getUserData” netlify function. THis block starts after the //TODO comment. Both functions work as expected before the refactoring.

What am i doing wrong?

This is the error message i get now:

Request from ::1: GET /.netlify/functions/getUserData
coins:  [ 'bitcoin', 'thorchain' ]
error TypeError: fetch2 is not a function
    at Object.exports.handler (/Users/***********/WebstormProjects/AssetDash/assetdash/src/functions/getUserData.js:40:23)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

This is a code snippet:

import faunadb from "faunadb";
const fetch = require("node-fetch");

require("encoding");

const q = faunadb.query;

const dbSecret = process.env.FAUNADB_SERVER_SECRET;

if (!dbSecret) {
  console.log(
    "Fant ikke credentials til databasen. Du må sette miljøvariabelen FAUNADB_SERVER_SECRET"
  );
  process.exit(1);
}

//Dette er et endepunkt
exports.handler = async (event) => {
  try {
    const client = new faunadb.Client({
      secret: dbSecret,
      domain: "db.eu.fauna.com",
    });

    const responseFromDb = await client.query(
      q.Map(
        q.Paginate(q.Documents(q.Collection("userData"))),
        q.Lambda((x) => q.Get(x))
      )
    );

    //TODO
    // funker ikke ĂĄ bruke fetch her. Hva er feil? egen mĂĄte ĂĄ kommunisere med functions internt?
    let cryptos = await responseFromDb.data[0].data.cryptos;
    let coins = [];
    cryptos.forEach((coin) => coins.push(coin.id));

    console.log("coins: ", coins);
    const res = await fetch(
      "/.netlify/functions/cryptoFunctions?coins=" + coins
    );
    console.log("res:", res);

    return {
      statusCode: 200,
      body: JSON.stringify(responseFromDb.data[0].data),
    };
  } catch (error) {
    console.log("error", error);
    return {
      statusCode: 500,
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(error),
    };
  }
};


While I don’t find a reference to fetch2 in your code so I don’t know where that’s coming from, but

this is incorrect.

In browser, you can use a relative URL and the browser can append the location.host value and send the request to the correct URL, but AWS lambda can’t do that. You’d have to use absolute URL in there. So, it should be like:

"https://my-site.netlify.app/.netlify/functions/cryptoFunctions?coins=" + coins`

(or you could use process.env.URL)

1 Like

@hrishikesh it works, I use this code to get data from another function:
var url = process.env.URL + '/.netlify/functions/utils?fun=truncate';var res = (await axios.get(url)).data

So I fetch the handler of the other function. From the handler I call a function within that function. Can I directly call that function without using the handler?

I’m not sure I understand your setup. Could you share an example?