【问题标题】:Error running fauna FQL query from netlify function从 netlify 函数运行动物群 FQL 查询时出错
【发布时间】:2021-04-01 15:33:04
【问题描述】:

我正在尝试从 netlify 函数运行查询。该功能非常简单,它只是“更新”一个帖子:

exports.handler = async function (event, context, callback) {
  const faunadb = require('faunadb')

  const headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
  }

  const q = faunadb.query
  const adminClient = new faunadb.Client({
    secret: process.env.FAUNADB_SERVER_SECRET,
  })

  const post = event.body

  const queryResult = await adminClient.query(
    q.If(
      q.Exists(q.Match(q.Index('post_uuid'), post.uuid)),
      q.Update(
        q.Select(['ref'], q.Get(q.Match(q.Index('post_uuid'), post.uuid))),
        { data: post }
      ),
      q.Create(q.Collection('posts'), { data: post })
    )
  )

  callback(null, {
    statusCode: 200,
    headers,
    body: '',
  })
}

查询使用 Fauna 的在线 shell 工作,脚本的所有其他部分似乎都在工作,但是当我运行查询时,我收到此错误并 netlify CLI 崩溃:

Request from ::1: POST /.netlify/functions/savePost
{"level":"error","message":"End - Error:"}
{"errorMessage":"validation failed","errorType":"BadRequest","level":"error"}
TypeError: Cannot read property 'join' of undefined
    at formatLambdaLocalError (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:25:100)
    at handleErr (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:29:55)
    at Context.callbackHandler [as callback] (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:60:14)
    at Context.done (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\lambda-local\build\lib\context.js:204:14)
    at Context.fail (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\lambda-local\build\lib\context.js:211:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
TypeError: Cannot read property 'join' of undefined
    at formatLambdaLocalError (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:25:100)
    at handleErr (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:29:55)
    at Context.callbackHandler [as callback] (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\src\utils\serve-functions.js:60:14)
    at Context.done (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\lambda-local\build\lib\context.js:204:14)
    at Context.fail (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\lambda-local\build\lib\context.js:211:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)

C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\netlify-redirector\lib\redirects.js:116
      throw ex;
      ^
abort({}) at Error
    at jsStackTrace (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\netlify-redirector\lib\redirects.js:1070:13)
    at stackTrace (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\netlify-redirector\lib\redirects.js:1087:12)
    at process.abort (C:\Users\Oliver\scoop\persist\nodejs\bin\node_modules\netlify-cli\node_modules\netlify-redirector\lib\redirects.js:8502:44)
    at process.emit (node:events:376:20)
    at emit (node:internal/process/promises:202:22)
    at processPromiseRejections (node:internal/process/promises:223:25)
    at processTicksAndRejections (node:internal/process/task_queues:94:32)
(Use `node --trace-uncaught ...` to show where the exception was thrown)

我在netlify dev 运行这个:

系统 视窗 10 节点:v15.4.0 netlify cli:v2.69.11

【问题讨论】:

    标签: netlify faunadb netlify-cli


    【解决方案1】:

    我发现你也可以用 try/catch 包裹整个东西并返回错误。

    exports.handler = async function (event, context, callback) {
      const faunadb = require('faunadb')
    
      const headers = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Headers': 'Content-Type',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
      }
    
      const q = faunadb.query
      const adminClient = new faunadb.Client({
        secret: process.env.FAUNADB_SERVER_SECRET,
      })
    
      const post = event.body
    
      try {
        const queryResult = await adminClient.query(
            q.If(
            q.Exists(q.Match(q.Index('post_uuid'), post.uuid)),
            q.Update(
                q.Select(['ref'], q.Get(q.Match(q.Index('post_uuid'), post.uuid))),
                { data: post }
            ),
            q.Create(q.Collection('posts'), { data: post })
            )
        )
    
        callback(null, {
            statusCode: 200,
            headers,
            body: '',
        })
      } catch (error) {
        callback(null, {
            statusCode: 500,
            headers,
            body: JSON.stringify(error),
        })
      }
    }
    

    【讨论】:

      【解决方案2】:

      好吧,我是个白痴!如果有人遇到与我相同的问题,我只是发布我自己问题的答案。

      首先(不知道为什么)看来你必须搬家

      const faunadb = require('faunadb')
      
      const q = faunadb.query
      const adminClient = new faunadb.Client({
        secret: process.env.FAUNADB_SERVER_SECRET,
      })
      

      函数之外。

      其次,查询失败是因为请求的主体,我称之为post 实际上是一个字符串而不是一个对象(有点明显,但我没有注意到它!)。我正在运行的查询需要将一个对象传递给CreateUpdate,但由于我传递了一个字符串而导致崩溃。

      这有点烦人(也许是一个错误?)但是如果查询返回一个 BadRequest,netlify 就会崩溃。

      【讨论】:

        猜你喜欢
        • 2022-11-24
        • 2023-03-03
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多