从netlify函数运行动物群FQL查询时出错



我正试图从netlify函数运行一个查询。功能非常简单,它只是";upserts";一篇文章:

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:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:25:100)
at handleErr (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:29:55)
at Context.callbackHandler [as callback] (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:60:14)
at Context.done (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_moduleslambda-localbuildlibcontext.js:204:14)
at Context.fail (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_moduleslambda-localbuildlibcontext.js:211:10)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
TypeError: Cannot read property 'join' of undefined
at formatLambdaLocalError (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:25:100)
at handleErr (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:29:55)
at Context.callbackHandler [as callback] (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clisrcutilsserve-functions.js:60:14)
at Context.done (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_moduleslambda-localbuildlibcontext.js:204:14)
at Context.fail (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_moduleslambda-localbuildlibcontext.js:211:10)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_modulesnetlify-redirectorlibredirects.js:116
throw ex;
^
abort({}) at Error
at jsStackTrace (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_modulesnetlify-redirectorlibredirects.js:1070:13)
at stackTrace (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_modulesnetlify-redirectorlibredirects.js:1087:12)
at process.abort (C:UsersOliverscooppersistnodejsbinnode_modulesnetlify-clinode_modulesnetlify-redirectorlibredirects.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.0netlify cli:v2.69.11

好吧,我是个白痴!万一有人遇到和我一样的问题,我只是发布一个我自己问题的答案。

首先(不确定为什么(似乎你必须移动

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

功能之外。

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

这有点烦人(也许有点bug?(但如果查询返回BadRequest,netlify就会崩溃。

我发现您还可以将整个过程封装在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),
})
}
}

相关内容

  • 没有找到相关文章

最新更新