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),
};
}
};