Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nuxt] [request error] [unhandled] [500] 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 Promise #381

Closed
rafaeldsousa opened this issue May 2, 2023 · 11 comments · Fixed by #403

Comments

@rafaeldsousa
Copy link

rafaeldsousa commented May 2, 2023

Environment

node: v16.17
nuxt: 3.4.2
nitro: 2.3.3

Reproduction

Nuxt API requests using useAsyncData + $fetch in frontend and server api uses await readBody

Describe the bug

frontend request

const body = { params: { ...route.query } }

const { data, error } = await useAsyncData(
    'server-api',
    () => $fetch('/api/server-api', { method: 'post', body: body }),
    {
        initialCache: false
    }
).catch((e) => console.error(e))

server API composable

export default defineEventHandler(async (event) => {

    const body = await readBody(event)
}

Additional context

Same issue as described here 19d133d by many others.
This latest release 1.4.0 seems to break things.

There is a workaround but it doesn't feel like something we'd actually want to be doing all over projects.
19d133d#r109674536

Logs

[nuxt] [request error] [unhandled] [500] 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 Promise
Copy link
Member

Would you be able to provide a reproduction? 🙏

@rafaeldsousa
Copy link
Author

@danielroe this would be sort of a clone in terms of npm packages and env setup I have, but I cannot reproduce the issue on codesandbox. (note I have some resolutions on package.json to force some version to what I have locally)
https://codesandbox.io/p/sandbox/hellow-rkt3in

Locally and on the azure server we use I can definitely still reproduce the issue when I upgrade to 1.6.4. (Video below)

Screen.Recording.2023-05-03.at.9.49.39.AM.mp4

@so1ve
Copy link

so1ve commented May 17, 2023

Found it first introduced in 19d133d

so1ve added a commit to so1ve/dolan-client-meme that referenced this issue May 17, 2023
@igorjacauna
Copy link
Contributor

igorjacauna commented May 21, 2023

I used this workaround, in commit that @so1ve mentioned, to make work my api routes. But, api routes created by libs like nuxt-vuefire for example, that has not the workaround implemented, throw the error.

PS: Error only shows up in production. I use Firebase to deploy.

@so1ve
Copy link

so1ve commented May 30, 2023

It suddenly worked in nuxt 3.5.1, and the workaround seemed to be buggy :(

@danielroe
Copy link
Member

@so1ve so is the issue resolved now?

@so1ve
Copy link

so1ve commented May 30, 2023

@so1ve so is the issue resolved now?

I'm not sure. In my use case (nuxt) the issue is resolved, but h3 users may not.

@igorjacauna
Copy link
Contributor

Hi @danielroe ,

I just run npx nuxi@latest upgrade --force and the Nuxt is in 3.5.2.

I got error about this issue that block my build. So, I removed yarn.lock and reinstall. And this time the build works.

Locally, the requests works fine. But in Firebase, where I deployed my application, shows:

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

The workaround still works:

// @ts-expect-error - req.body is not defined in the type definitions
const body = await (event.node.req.body || readBody(event));

@igorjacauna
Copy link
Contributor

igorjacauna commented May 30, 2023

@danielroe , just created this Nuxt project with npx nuxi@latest init app: https://github.com/igorjacauna/issue-readbody

After deploy to Firebase, should show error when try to make a POST request to /api/issue with a body in request

In Function logs shows:

TypeError [ERR_INVALID_ARG_TYPE]: 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
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at file:///workspace/node_modules/h3/dist/index.mjs:288:70
    at async readBody (file:///workspace/node_modules/h3/dist/index.mjs:314:16)
    at async file:///workspace/chunks/issue.post.mjs:6:18 {
  code: 'ERR_INVALID_ARG_TYPE'
}

@sisou
Copy link
Contributor

sisou commented May 31, 2023

Thanks @igorjacauna for the repro!

It even works (or rather breaks) without deploying to Firebase. Change API_ID in .firebaserc to something lowercase (like appid) and just add firebase-tools as a dependency, and you can run and reproduce it in the emulator:

yarn add --dev firebase-tools
NITRO_PRESET=firebase yarn build
yarn firebase emulators:start --only functions,hosting

# From another terminal:
curl -X POST 'http://localhost:5000/api/issue' -H 'Content-Type: application/json' --data-raw '{"test":1}'

@pi0
Copy link
Member

pi0 commented Jan 3, 2024

Another related fix where firebase parses form-data requests as object into req.body: #604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants