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

data in "before create" hooks is empty #80

Closed
invakid404 opened this issue Jul 11, 2022 · 3 comments
Closed

data in "before create" hooks is empty #80

invakid404 opened this issue Jul 11, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@invakid404
Copy link

invakid404 commented Jul 11, 2022

While playing around with using PocketBase as a framework, I noticed that the User in the OnUserBeforeCreateRequest hook is empty. Let's take the following hook as an example:

	app.OnUserBeforeCreateRequest().Add(func(event *core.UserCreateEvent) error {
		encoder := json.NewEncoder(os.Stdout)
		encoder.SetIndent("", "  ")

		return encoder.Encode(event.User)
	})

When I create a user, I get the following output:

image

All fields appear to be empty. By stepping through the "user create" endpoint with a debugger, I observed this:

  1. The request body is parsed correctly in the form, but
  2. Those fields aren't set in the user object yet

image

Reading through the code, I confirmed that the user struct's fields don't get touched until Submit() is called on the form: here.

And since Submit() isn't called until the OnUserBeforeCreateRequest hooks have finished executing, the hooks don't get access to the user that's about to be created.

This definitely doesn't sound like expected behavior (otherwise that hook would be very useless indeed). Without really knowing how this project is structured, it seems like the field setting portion of Submit() should happen earlier somewhere.

This appears to be the case with other "before create" hooks. I also tried the "OnRecordBeforeCreateRequest" hook, but the record in there has no data whatsoever either, besides the "@collectionId" and "@collectionName" fields. Calling .Data() on it doesn't help much either, a blank map is returned.

@invakid404 invakid404 changed the title data in "before create" events is empty data in "before create" hooks is empty Jul 11, 2022
@ganigeorgiev ganigeorgiev added the bug Something isn't working label Jul 11, 2022
@ganigeorgiev
Copy link
Member

ganigeorgiev commented Jul 11, 2022

Thank you for the detailed report. This is indeed a bug that I've introduced a week before the release (initially the events were operating on the form instance which is populated before triggering the hook).

It'll be fixed in the next v0.3.0 version (together with the nullable field controls).

@ganigeorgiev
Copy link
Member

ganigeorgiev commented Jul 12, 2022

I've decided to release the fix for this in v0.2.4 because it could be a blocker if someone is using PocketBase as a framework.

The form request unit tests were update to reflect the change, but we'll have to think how to improve the integration tests for the api, because currently we are verifying only whether the hook was triggered or not, but there is no easy way to test the hook(s) data.

@invakid404 Please test it and if you stumble on something, feel free to let me know and I'll investigate it further.

@invakid404
Copy link
Author

@ganigeorgiev thanks for the fast response, I can confirm that the OnUserBeforeCreateRequest hook works as expected now. Using the same hook as an example:
image

The password hash isn't shown in the JSON output, but I've checked manually, and it's properly set as well. Looks good to me so far :^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants