I am trying to pass props to an SvelteKit endpoint async function with no success. I'm using a store to pass value, but for reasons I don't understand value is undefined when I try to get it within the function.
Can anyone see what I'm doing wrong, either accessing the store value or if there is a better way to pass value to the function? Thanks! Eric
index.svelte
<script lang="ts">
import { sampleData } from "~/data/sample";
async function handleClick() {
$sampleData = {value: "Fancy"};
const result = await (await fetch(`/apis/ping`)).json();
console.log("result", result);
}
</script>
<button on:click={handleClick}>Ping</button>
ping.ts
import { sampleData } from "~/data/sample";
import { get as getDoc } from "svelte/store";
export async function get(): Promise<{ body: any }> {
const _sampleData = getDoc(sampleData);
const value = _sampleData.value;
console.log("value", value);
// const value = "Fancy";
const result = await (
await fetch(`https://my-server/app/ping?value=${value}`)
).json();
console.log("result", result);
return {
body: result,
};
}