如何在 Nuxt 3 server/api 中传递参数?
How to pass params in Nuxt 3 server/api?
我不知道如何在 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
};
你知道如何将参数传递给 Nuxt 3 server/api吗?或者你有资源吗?此时的official docs are blank。
我认为您不能像现在这样将参数直接传递给函数。
在 another part of the docs 中,它表示当您将主体传递给 server/api
函数时,您需要使用 await useBody(event)
.
检索它
使用useBody
它在文档中的提及:https://v3.nuxtjs.org/guide/features/server-routes#handling-requests-with-body
你只需要通读
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
};
我不知道如何在 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
};
你知道如何将参数传递给 Nuxt 3 server/api吗?或者你有资源吗?此时的official docs are blank。
我认为您不能像现在这样将参数直接传递给函数。
在 another part of the docs 中,它表示当您将主体传递给 server/api
函数时,您需要使用 await useBody(event)
.
使用useBody
它在文档中的提及:https://v3.nuxtjs.org/guide/features/server-routes#handling-requests-with-body
你只需要通读
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
};