我正在开发一个 Nuxt 3 项目,这是我第一次关注服务器端。我已经创建了一个服务器路由,我在 Pinia 商店中调用它。在我的本地机器上它按预期工作。但是当我部署到 Firebase 托管(产品)时,我收到 500 错误......
我得到的错误是:
"url": "/api/chat-completion",
"statusCode": 500,
"statusMessage": "",
"message": "The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object",
"stack": ""
}
我在 Pinia 商店中调用我的 Nuxt 服务器端点,如下所示:
async function doSomething(formData: ApplicationInfo) {
if (userStore.userData.credits > 0) {
try {
const data = await $fetch<string>('/api/chat-completion', {
method: 'POST',
body: formData
})
if (data) {
// do stuff
}
} catch (e) {
}
} else {
}
}
我创建了:
server/api/chat-completion.ts
,看起来像这样:
import { ApplicationInfo } from '../../types/applicationInfo.interface'
export default defineEventHandler(async (event): Promise<string> => {
const body: ApplicationInfo = await readBody<ApplicationInfo>(event)
const response = await chatCompletion(body)
return response
})
ApplicationInfo 是一个对象。
通过删除 DefineEventHandler() 函数中的所有内容并将代码一一放回,我已经能够找出问题出在 readBody() 函数上。
我遵循了https://nuxt.com/docs/guide/directory-struct/server#handling-requests-with-body中的确切代码,它适用于本地。当我部署到 Firebase 托管上的生产环境时,它不起作用...
有人可以帮我解决这个问题吗?
试试这个!
const body = event.node.req.body || await readBody(event);