Netlify Functions: network timeout at: https://db.fauna.com/"

I have created a Netlify Function (Lambda) which creates new document in database. It works on localhost (using netlify dev), but I receive a “network timeout” error after code is uploaded to Netlify:

5:32:27 PM: 2020-12-29T16:32:27.124Z	6b6b680d-ca66-4cdc-9cd1-a9289849ef46 ERROR	Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"FetchError: network timeout at: https://db.fauna.com/","reason":{"errorType":"FetchError","errorMessage":"network timeout at: https://db.fauna.com/","message":"network timeout at: https://db.fauna.com/","type":"request-timeout","stack":["FetchError: network timeout at: https://db.fauna.com/"," at Timeout.<anonymous> (/var/task/src/node_modules/node-fetch/lib/index.js:1454:13)"," at listOnTimeout (internal/timers.js:554:17)"," at processTimers (internal/timers.js:497:7)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: FetchError: network timeout at: https://db.fauna.com/"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
5:32:27 PM: [ERROR] [1609259547134] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 403.
5:32:27 PM: Duration: 20.30 ms	Memory Usage: 76 MB
5:32:27 PM: RequestId: 6b6b680d-ca66-4cdc-9cd1-a9289849ef46 Error: Runtime exited with error: exit status 128
Runtime.ExitError

Function code:

// Create new user profile in Fauna database

var faunadb = require('faunadb'),
  q = faunadb.query

exports.handler = async function(event, context) {
    var client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET })
    
    const email = event.queryStringParameters.email 
    const name = event.queryStringParameters.name
    const accountName = event.queryStringParameters.accountName
    const accountId = event.queryStringParameters.accountId
    const propertyName = event.queryStringParameters.propertyName
    const propertyId  = event.queryStringParameters.propertyId
    const propertyTimezone  = event.queryStringParameters.propertyTimezone
    const dataset = event.queryStringParameters.dataset 

    var createP = client.query(
        q.Create(
          q.Collection('users'),
          { data: { 
              email: email,
              name: name,
              account: {
                  name: accountName,
                  id: accountId,
              },
              property: {
                name: propertyName,
                id: propertyId,
                timezone: propertyTimezone,
              },
              dataset: dataset,
            } 
          }
        )
      )

    return {
        statusCode: 200,
        body: ""
    };
}

Any ideas what might be wrong here?

After clearing cache and redeploying code, error changed to “Client network socket disconnected before secure TLS connection was established”:

6:29:23 PM: 2020-12-29T17:29:23.893Z	0fc6ed22-c45d-4fc6-a38e-d374518b8879	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"FetchError: request to https://db.fauna.com/ failed, reason: Client network socket disconnected before secure TLS connection was established","reason":{"errorType":"FetchError","errorMessage":"request to https://db.fauna.com/ failed, reason: Client network socket disconnected before secure TLS connection was established","code":"ECONNRESET","message":"request to https://db.fauna.com/ failed, reason: Client network socket disconnected before secure TLS connection was established","type":"system","errno":"ECONNRESET","stack":["FetchError: request to https://db.fauna.com/ failed, reason: Client network socket disconnected before secure TLS connection was established","    at ClientRequest.<anonymous> (/var/task/src/node_modules/node-fetch/lib/index.js:1461:11)","    at ClientRequest.emit (events.js:314:20)","    at TLSSocket.socketErrorListener (_http_client.js:428:9)","    at TLSSocket.emit (events.js:314:20)","    at emitErrorNT (internal/streams/destroy.js:92:8)","    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)","    at processTicksAndRejections (internal/process/task_queues.js:84:21)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: FetchError: request to https://db.fauna.com/ failed, reason: Client network socket disconnected before secure TLS connection was established","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:314:20)","    at processPromiseRejections (internal/process/promises.js:209:33)","    at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}

@patrykkalinowskis, sounds like an issue with your Netlify production deployment?

I think the issue is you aren’t waiting for the promise to finish before returning data from your lambda.

Try putting an await here: await client.query like this:

// Create new user profile in Fauna database

var faunadb = require('faunadb'),
  q = faunadb.query

exports.handler = async function(event, context) {
    var client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET })
    
    const email = event.queryStringParameters.email 
    const name = event.queryStringParameters.name
    const accountName = event.queryStringParameters.accountName
    const accountId = event.queryStringParameters.accountId
    const propertyName = event.queryStringParameters.propertyName
    const propertyId  = event.queryStringParameters.propertyId
    const propertyTimezone  = event.queryStringParameters.propertyTimezone
    const dataset = event.queryStringParameters.dataset 

    await client.query(
        q.Create(
          q.Collection('users'),
          { data: { 
              email: email,
              name: name,
              account: {
                  name: accountName,
                  id: accountId,
              },
              property: {
                name: propertyName,
                id: propertyId,
                timezone: propertyTimezone,
              },
              dataset: dataset,
            } 
          }
        )
      )

    return {
        statusCode: 200,
        body: ""
    };
}
1 Like

Thank you @wallslide, that worked perfectly :slight_smile:
My async javascript is not something I’m proud of :wink: