首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从netlify函数运行fauna查询时出错

从netlify函数运行fauna查询时出错
EN

Stack Overflow用户
提问于 2020-12-23 07:09:56
回答 2查看 204关注 0票数 0

我正在尝试从netlify函数运行查询。这个函数非常简单,它只是“upsert”一个post:

代码语言:javascript
复制
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崩溃:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2021-02-15 04:56:03

我发现您还可以将整个过程包装在try/catch中并返回错误。

代码语言:javascript
复制
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),
    })
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-12-23 21:01:06

好吧,我是在做一个笨蛋!如果有人遇到了和我一样的问题,我只是发布了我自己问题的答案。

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

代码语言:javascript
复制
const faunadb = require('faunadb')

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

在函数的外部。

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

这有点烦人(可能有一点bug?)但是如果查询返回一个BadRequest,netlify就会崩溃。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65417015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档