Async Serverless Function Trouble

Site - selftaught-dev.com
Site ID - happy-hugle-97373a

For some reason I’m gettinng a "TypeError: n is not a function"
I’ve narrowed it down to this line const result = faunaFetch({ query, variables }) through adding console logs before/after it. Is there anything glaringly wrong with that line, or in the faunaFetch function?

Full Function Below

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY)
const fetch = require("node-fetch")

exports.handler = async (event, context) => {
  const { user } = context.clientContext

  const faunaFetch = async ({ query, variables }) => {
    return await fetch("https://graphql.fauna.com/graphql", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.FAUNA_SERVER_KEY}`,
      },
      body: JSON.stringify({
        query,
        variables,
      }),
    })
      .then(res => res.json())
      .catch(err => console.error(JSON.stringify(err, null, 2)))
  }

  const query = `
    query ($netlifyID: ID!) {
      getUserByNetlifyID(netlifyID: $netlifyID){
        stripeID
        netlifyID
      }
    }
  `
  const variables = { netlifyID: user.sub }

  const result = faunaFetch({ query, variables })

  console.log(JSON.stringify(result))
  // console.log(stripeID)

  return {
    statusCode: 200,
    body: JSON.stringify(result),
  }
}

Function Log

8:51:11 AM: 2020-10-24T13:51:11.978Z	305c10be-c371-40c2-bd43-be0553b9573c	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: n is not a function","reason":{"errorType":"TypeError","errorMessage":"n is not a function","stack":["TypeError: n is not a function","    at /var/task/src/create-manage-link.js:1:70055","    at Runtime.t.handler (/var/task/src/create-manage-link.js:1:70284)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: n is not a function","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:315:20)","    at processPromiseRejections (internal/process/promises.js:209:33)","    at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
8:51:11 AM: [ERROR] [1603547471979] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 403.
8:51:11 AM: Duration: 7.52 ms	Memory Usage: 67 MB	Init Duration: 187.74 ms	
8:51:11 AM: RequestId: 305c10be-c371-40c2-bd43-be0553b9573c Error: Runtime exited with error: exit status 128
Runtime.ExitError

Repo

Function is in src → functions → create-manage-link.js

Would you need to await the faunafetch call?

const result = await faunaFetch({ query, variables })

since the function is async :slight_smile:

I’ve tried that as well but still get the same error.

10:24:15 AM: 2020-10-24T15:24:15.573Z	0fd1129e-bce4-4b8c-b7ac-c9c12a75d24c	ERROR	Invoke Error 	{"errorType":"TypeError","errorMessage":"n is not a function","stack":["TypeError: n is not a function","    at /var/task/src/create-manage-link.js:1:70080","    at Runtime.t.handler (/var/task/src/create-manage-link.js:1:70309)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
10:24:15 AM: Duration: 4.96 ms	Memory Usage: 67 MB	Init Duration: 163.40 ms	

Have you tried returning with a callback rather than just a return?
faunaFetch({ query, variables }).then((data) => {callback(null, { statusCode: 200, body: JSON.stringify(result) })});

or something similar, bare in mind brackets might be wrong as I typed it directly here.

EDIT: I just realised that you have a 403, maybe this could be causing an issue too as you don’t have access to the POST request? Maybe the auth isnt working? (Although youd still want to pass that back to the frontend rather than log it)