admin管理员组文章数量:1418636
I have the following setup in my +page.server.js:
export const actions = {
createScore: async ({ request }) => {
const values = await request.formData();
try {
const instagram = values.get('username');
const score = Number(values.get('score'));
await prisma.score.create({
data: {
score,
instagram
}
});
return { success: true };
} catch (err) {
console.error(err);
return fail(500, { message: 'Could not create the score.' });
}
}
};
This runs fine, I see the entry in the database. However, no matter how hard I try, I cannot access 'form' to check for the return of sucess in my front end.
I do the following in page.svelte
<script>
export let form;
console.log(form);
</script>
The console log consistently returns null when the form is run.
Further, the following never renders (telling me I am not missing a state change):
{#if form?.success}
<p>It Worked</p>
{/if}
Finally, form as follows:
<form action="?/createScore" method="POST">
<input type="text" id="score" name="score" />
<input type="text" id="username" name="username" />
<button type="submit">Submit</button>
</form>
Am I missing something?
I have the following setup in my +page.server.js:
export const actions = {
createScore: async ({ request }) => {
const values = await request.formData();
try {
const instagram = values.get('username');
const score = Number(values.get('score'));
await prisma.score.create({
data: {
score,
instagram
}
});
return { success: true };
} catch (err) {
console.error(err);
return fail(500, { message: 'Could not create the score.' });
}
}
};
This runs fine, I see the entry in the database. However, no matter how hard I try, I cannot access 'form' to check for the return of sucess in my front end.
I do the following in page.svelte
<script>
export let form;
console.log(form);
</script>
The console log consistently returns null when the form is run.
Further, the following never renders (telling me I am not missing a state change):
{#if form?.success}
<p>It Worked</p>
{/if}
Finally, form as follows:
<form action="?/createScore" method="POST">
<input type="text" id="score" name="score" />
<input type="text" id="username" name="username" />
<button type="submit">Submit</button>
</form>
Am I missing something?
Share Improve this question edited Mar 27, 2023 at 14:53 NickP asked Mar 27, 2023 at 14:35 NickPNickP 1,4142 gold badges25 silver badges57 bronze badges 9-
Show the
form
that triggers the action. – brunnerh Commented Mar 27, 2023 at 14:47 -
Also, if anything, you would want to log reactively:
$: console.log(form);
– brunnerh Commented Mar 27, 2023 at 14:48 -
1
I do not see any issues here. The page file is named
+page.svelte
, correct? Also, would remend using theenhance
action to not fully reload the page on submit. – brunnerh Commented Mar 27, 2023 at 15:00 - 3 It is but strangely, enhance seems to have done it and I am now getting something back! Thanks – NickP Commented Mar 27, 2023 at 15:03
- 1 Ran into same issue today, adding use:enhance solved it. Not sure why it was necessary though – Sebastian Commented Aug 30, 2023 at 20:39
3 Answers
Reset to default 1I cannot access 'form' to check for the return of success in my front end.
Maybe different in your case, but I came across the same issue once when I first started learning SvelteKit.
In my case, I had turned off server-side rendering in a +layout.js
file so that I could access the window
object on the frontend:
export const ssr = false;
(I got that here.)
Commenting out that line (along with re-working the way that I handled localstorage access) solved my issue.
I had the same problem. I could fix it by moving the Form
from a ponent to +page.svelte
.
My issue was that I had a variable with the same name as the actionData. When I changed it it worked.
本文标签: javascriptSvelteKit Form Action not returning successremains nullStack Overflow
版权声明:本文标题:javascript - SvelteKit Form Action not returning successremains null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745291131a2651802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论