Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

SvelteKIt: getting an error "Handler should return a response", though I am returning one?

$
0
0

I have a simple API endpoint server function that imitates an authorization POST request:

/** @type {import('./$types').RequestHandler} */import { json, error } from '@sveltejs/kit'export async function POST({ request }) {    const data = await request.json()    if (!data.username || !data.password) {        return error(400, 'Missing username or password.')    }    return json({ username: data.username, id: 1 })}

And a form that uses this function in +page.svelte:

async function login() {        const response = await fetch('/api/auth/login', {            method: 'POST',            body: JSON.stringify({                username,                password            })        })        const resJSON = await response.json()        console.log('Form submitted', resJSON)    }

But I'm getting an error in the terminal: Error: Invalid response from route /api/auth/login: handler should return a Response object.

Am I not returning an object in json({})?


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>