Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
How to pass params in Nuxt 3 server/api?

I can't figure it out how to pass params to an anonymous function in Nuxt 3.

index.vue:

<template>
  <form @submit.prevent="signUpNewsletter()">
    <input type="email" placeholder="example@x.com" v-model="userEmail">
    <input type="submit" value="Submit">
  </form>
</template>

<script setup>
const userEmail = ref('x@x.de')

function signUpNewsletter () {
  useAsyncData(
    'newsletter',
    () => $fetch('/api/sign_up_news', {
      method: 'POST', // Post method works
      body: {
        email: userEmail.value
      }
    })
  )
}
</script>

server/api/sign_up_news.js:

import { createClient } from '@supabase/supabase-js'

export default async (email) => { // can't read the parameter
  const SUPABASE_KEY = 'key123'
  const SUPABASE_URL = 'url.supabase.co'
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)

  const { data, error } = await supabase
    .from('newsletter')
    .insert([{ email }]) // <<< Fails!
  return data
};

working:

import { createClient } from '@supabase/supabase-js'

export default async () => {
  const SUPABASE_KEY = 'key123'
  const SUPABASE_URL = 'url.supabase.co'
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)

  const { data, error } = await supabase
    .from('newsletter')
    .insert([{ email: 'hi@it.works' }]) // <<< Works!
  return data
};

Do you know how to pass parameter into Nuxt 3 server/api? Or do you got a source? The official docs are blank at this moment.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I don't think you're able to pass params directly into the functions the way you're doing.

In another part of the docs, it says that when you pass a body into the server/api function, you'll need to retrieve it using await useBody(event).

about 4 years ago · Juan Pablo Isaza Relatório

0

Use useBody

Its mention in the docs: https://v3.nuxtjs.org/guide/features/server-routes#handling-requests-with-body

you just need to read through

import { createClient } from '@supabase/supabase-js'

export default async (event) => { // can't read the parameter
  
  const body = await useBody(event)
  const SUPABASE_KEY = 'key123'
  const SUPABASE_URL = 'url.supabase.co'
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)

  const { data, error } = await supabase
    .from('newsletter')
    .insert([{ email: body.email }]) 
  return data
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda