Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Tutorial? #494

Closed
thunderbug1 opened this issue Oct 28, 2024 · 2 comments
Closed

Bug in Tutorial? #494

thunderbug1 opened this issue Oct 28, 2024 · 2 comments

Comments

@thunderbug1
Copy link

thunderbug1 commented Oct 28, 2024

I am going through the Tutorial of the CRUD example here

It has this code passage:

import { superValidate, message } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { error, fail, redirect } from '@sveltejs/kit';
import { users, userId } from '$lib/users';

export const load = async ({ url, params }) => {
  // READ user
  const user = users.find((u) => u.id == params.id);

  if (params.id && !user) throw error(404, 'User not found.');

  // If user is null, default values for the schema will be returned.
  const form = await superValidate(user, zod(crudSchema));
  return { form, users };
};

What is confusing to me is this comment: // If user is null, default values for the schema will be returned.
It is not true, because when the user is null we throw an error in the line above and so we won't return an object with the default values.
How does this work?

@thunderbug1 thunderbug1 added the bug Something isn't working label Oct 28, 2024
@ciscoheat
Copy link
Owner

params.id is the key here, so if you don't specify an id in the route the user will be null, which will set up an empty form for creating a new user.

@ciscoheat ciscoheat removed the bug Something isn't working label Nov 1, 2024
@thunderbug1
Copy link
Author

Ah ok, thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants