Skip to content

Svelte 5: TypeError: null is not an object (evaluating 'metadata.parent') when assigning value to $bindable #11204

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

Closed
Torbet opened this issue Apr 17, 2024 · 6 comments · Fixed by #11217
Assignees
Milestone

Comments

@Torbet
Copy link

Torbet commented Apr 17, 2024

Describe the bug

Everything works as expected, until calling select on a result.

I get the error TypeError: null is not an object (evaluating 'metadata.parent') from get_owner when assigning place = result.

console.loging result gives me a Proxy({...result})

Tried with result.id and results.find(...) but same error, I think its localised to the actual assigning part.

Reproduction

<script lang="ts">
	let { place = $bindable() }: { place: Place | null } = $props();

	let query: string | null = $state(null);
	let results: Place[] = $state([]);

    $inspect(place)

	const search = async () => {
		if (!query || query.length < 3) return;

		const response = await fetch(`/api/places?q=${query}`);
		results = await response.json();
	};

	const select = (result: Place | null) => {
		place = result;
		query = null;
		results = [];
	};
</script>

<label class="input input-bordered flex items-center gap-2">
	<svg
		width="24"
		height="28"
		xmlns="http://www.w3.org/2000/svg"
		fill="none"
		viewBox="0 0 24 24"
		stroke-width="1.5"
		stroke="currentColor"
		class="opacity-70"
	>
		<path
			stroke-linecap="round"
			stroke-linejoin="round"
			d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z"
		/>
	</svg>
	<input
		type="text"
		bind:value={query}
		oninput={search}
		placeholder="Search bars and pubs..."
		class="grow"
	/>
</label>

{#if results.length || place}
	<ul class="menu">
		{#if place}
			<li>
				<button onclick={() => select(null)} class="active flex flex-col items-start">
					<span class="font-bold">{place.name}</span>
					<span class="text-sm text-gray-500">{place.street}, {place.city}, {place.country}</span>
				</button>
			</li>
		{:else}
			{#each results as result}
				<li>
					<button onclick={() => select(result)} class="flex flex-col items-start">
						<span class="font-bold">{result.name}</span>
						<span class="text-sm text-gray-500"
							>{result.street}, {result.city}, {result.country}</span
						>
					</button>
				</li>
			{/each}
		{/if}
	</ul>
{/if}

Logs

`TypeError: null is not an object (evaluating 'metadata.parent')`

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 171.11 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.7.2 - /opt/homebrew/bin/node
    npm: 10.5.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 123.0.6312.124
    Safari: 17.4.1
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.0.0-next.107

Severity

annoyance

@Torbet
Copy link
Author

Torbet commented Apr 17, 2024

Logging more info with $inspect, place successfully updates with the new data, but the error fires after and the page does not update like in svelte 4.

@dummdidumm dummdidumm added this to the 5.0 milestone Apr 17, 2024
@dummdidumm dummdidumm self-assigned this Apr 17, 2024
@Torbet
Copy link
Author

Torbet commented Apr 17, 2024

Edit 2 @dummdidumm :

It's the $bindeable causing the issue! having a regular let test: Place | null = $state(null) and assigning to that works fine

@Torbet Torbet changed the title Svelte 5: TypeError: null is not an object (evaluating 'metadata.parent') with $state array - Proxy? Svelte 5: TypeError: null is not an object (evaluating 'metadata.parent') when assigning value to $bindable Apr 17, 2024
@chainlist
Copy link

I have the same error using $state([]) so it might not be related that much to $bindable

@chainlist
Copy link

chainlist commented Apr 17, 2024

@dummdidumm I made a very minimal reproductible demo (without $bindable)

The buttons should be self-explenatory.

From what I understand, the error is raised when I try to edit one of the properties of an object stored in a rune that is itself passed as a $props() to a component.

@ChristopheCorbalan
Copy link

ChristopheCorbalan commented Apr 18, 2024

@dummdidumm I made a very minimal reproductible demo (without $bindable)

The buttons should be self-explenatory.

From what I understand, the error is raised when I try to edit one of the properties of an object stored in a rune that is itself passed as a $props() to a component.

@chainlist The issue appears as well without passing the rune object to another component — REPL

This issue started since [email protected].

@chainlist
Copy link

chainlist commented Apr 18, 2024

Indeed @ChristopheCorbalan.

I suspected as well it was from #11184

dummdidumm added a commit that referenced this issue Apr 18, 2024
Ownership was not widened when assigning a sub state to a different top level state. The set of owners for the state was zero because the owner was on the original parent, but that one was reset to null because it's now the top level of a different state. That meant that there was no owner but also no parent to check for the owner, which is an invalid combination resulting in a nullpointer (and also potentially false positive warnings in other situations).

fixes #11204
dummdidumm added a commit that referenced this issue Apr 18, 2024
Ownership was not widened when assigning a sub state to a different top level state. The set of owners for the state was zero because the owner was on the original parent, but that one was reset to null because it's now the top level of a different state. That meant that there was no owner but also no parent to check for the owner, which is an invalid combination resulting in a nullpointer (and also potentially false positive warnings in other situations).

fixes #11204
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

Successfully merging a pull request may close this issue.

4 participants