netlify / netlify-faunadb-example

Using FaunaDB with netlify functions
https://www.netlify.com/blog/2018/07/09/building-serverless-crud-apps-with-netlify-functions--faunadb/
385 stars 121 forks source link

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'statusCode' of undefined #9

Open abtx opened 5 years ago

abtx commented 5 years ago

Hi, thanks for this great little example, it helped me get started quickly and get better understanding of how to use Netlify's implementation of Lambda.

Something I wanted to share as well is I've set up the secret in .env and added export $(cat .env | xargs) && to the start script which is handy for local dev.

As to the error - I find that if you unwrap your lambda functions return responses from the callback objects the error is fixed: console.log('error', error) return { statusCode: 200, body: JSON.stringify(ret) }

I thought sharing this might help someone bootstrap this project. Thanks!

kluplau commented 5 years ago

Instead of running npm run start, try using netlify dev. It's Netlify's CLI that runs the functions as they are run when pushed to production.

JeffML commented 5 years ago

netlify dev gives me a different error when loading the page.

Request from ::1: GET /.netlify/functions/todos-read-all
Function `todo-read-all` invoked
(node:6925) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Todo refs [
  Ref(Collection("todos"), "243239304743092745"),
  Ref(Collection("todos"), "243239497348678153"),
  Ref(Collection("todos"), "243241612879069710")
]
3 todos found
Response with status 200 in 418 ms.
◈ Error during invocation:  lambda response was undefined. check your function code again.
/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
◈ Error during invocation:  lambda response was undefined. check your function code again.
/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
JeffML commented 5 years ago

It has all the symptoms of a callback being called twice, once with the correct parameters, and the second with no parameters. Not able to track it down, though.

franva commented 4 years ago

It has all the symptoms of a callback being called twice, once with the correct parameters, and the second with no parameters. Not able to track it down, though.

I am facing the same issue. How did you solve it?

JeffML commented 4 years ago

@franva it was nearly a year ago, so I can't remember the specifics, but I believe I had my netlify lambda folder set up wrong, or was skipping a step. I wrote up what I did to finally get it working here: https://www.freecodecamp.org/news/netlify-functions-firebase-and-graphql-working-together-at-last/