prisma + svelte : Cannot use keyword 'await' outside an async function #8610
-
| 
         Hi . I am newbie to nodejs / sveltelkit. I am tring to make very simple CRUD using sveltekit/ prisma this is create script. // ./src/routes/api/tasks/create/+server.js
import { error } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client'
/** @type {import('./$types').RequestHandler} */
export async function GET({ url }) {
    const prisma = new PrismaClient()
    const task = await create(prisma);
    return new Response(JSON.stringify(task));
}
async function create(prisma) {
    const task = await prisma.tasks.create({
        data: {
            title: 'Task 1',
        },
    })
    return task
}accessing to http://localhost:5173/api/tasks/create is very fine. It create new task model. And this is the code for show model. //  ./src/routes/api/tasks/+server.js
import { error } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client'
/** @type {import('./$types').RequestHandler} */
export function GET({ url }) {
    const prisma = new PrismaClient()
    const task = await show(prisma);
    return new Response(JSON.stringify(task));
}
async function show(prisma) {
    const task = await prisma.tasks.findUnique({
        where: {
            id: 1,
        },
    })
    return task
}but when I try to access to http://localhost:5173/api/tasks give me this error. Error: Parse failure: Cannot use keyword 'await' outside an async function (11:17)
Contents of line 11: const task = await show(prisma);I just changed from 'create' to 'show' and from 'create' to 'findUnique' . Why do I have this error and how can I fix it ?  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
| 
         If you use TypeScript, your editor will yell at you & ask you to mark the  //  ./src/routes/api/tasks/+server.js
import { error } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client'
/** @type {import('./$types').RequestHandler} */
-export function GET({ url }) {
+export async function GET({ url }) { | 
  
Beta Was this translation helpful? Give feedback.
If you use TypeScript, your editor will yell at you & ask you to mark the
function GETto be async.// ./src/routes/api/tasks/+server.js import { error } from '@sveltejs/kit'; import { PrismaClient } from '@prisma/client' /** @type {import('./$types').RequestHandler} */ -export function GET({ url }) { +export async function GET({ url }) {