0

我正在尝试从 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

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

4

2 回答 2

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),
    })
  }
}
于 2021-02-14T20:56:03.007 回答
0

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

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

const faunadb = require('faunadb')

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

函数之外。

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

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

于 2020-12-23T13:01:05.660 回答