0

I am writing a serverless netlify function when I hit /:uid endpoint, it shows me this error message n is not a function but when I hit / endpoint, it doesn't throw an error. Please help me with this.

src/api.js file code

const express = require("express");
const cors = require("cors");
const fetch = require("node-fetch");
const helmet = require("helmet");
const serverless = require("serverless-http");

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

const app = express();
const router = express.Router();
app.use(cors());
app.use(helmet());

const fetchWithToken = (endpoint, method = "GET") => {
  return fetch(`${process.env.API_BASE_URL}${endpoint}`, {
    method,
    headers: {
      Authorization: `token ${process.env.TOKEN}`,
    },
  });
};

router.get("/", (req, res) => {
  res.send("JDKJKFJ");
});

router.get("/:uid", async (req, res) => {
  try {
    const data = await fetchWithToken(`/${req.params.uid}`, "GET");
    res.status(200).json(data);
  } catch (error) {
    console.log(error.message);
    res.status(500).json({ error });
  }
});

app.use("/.netlify/functions/api", router);

module.exports.handler = serverless(app);

error message

TypeError: n is not a function
    at /Users/rishipurwar/codingspace-proxy-server/functions/api.js:159:3133
    at /Users/rishipurwar/codingspace-proxy-server/functions/api.js:159:3232
    at o.handle_request (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:120:783)
    at o (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:113:879)
    at d.dispatch (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:113:901)
    at o.handle_request (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:120:783)
    at /Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:2533
    at f (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:3502)
    at f (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:3696)
    at Function.v.process_params (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:3839)
    at g (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:2476)
    at Function.v.handle (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:3340)
    at p (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:238)
    at o.handle_request (/Users/rishipurwar/codingspace-proxy-server/functions/api.js:120:783)
    at /Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:2905
    at /Users/rishipurwar/codingspace-proxy-server/functions/api.js:106:2927
Response with status 500 in 57 ms.
r121
  • 2,478
  • 8
  • 25
  • 44

1 Answers1

0

You need to add .json() to the returned value from fetch:

try {
    let data = await fetchWithToken(`/${req.params.uid}`, "GET");
    data = await data.json()
    res.status(200).json(data);
  } catch (error) {
    console.log(error.message);
    res.status(500).json({ error });
  }
  • still getting same error message – r121 Nov 01 '21 at 15:14
  • I don't know why I am also not able to access env inside the api.js file – r121 Nov 01 '21 at 15:16
  • 1
    @RishiPurwar I just checked your repo, and I saw that you're using netlify-lambda to run your program. I think the solution would be to change the way you are using the env variables, you can check a detailed guide on this [Github issue answer](https://github.com/netlify/netlify-lambda/issues/118#issuecomment-506973346) – salah-chaouch Nov 01 '21 at 15:31
  • Is it possible for you to write a proper answer? so that It'll be helpful for someone in the future – r121 Nov 01 '21 at 15:37
  • If I directly use the values instead of using env variables. still, it's showing me an error. – r121 Nov 01 '21 at 15:38