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

Components render as undefined? #21

Closed
fubz opened this issue Sep 11, 2024 · 1 comment
Closed

Components render as undefined? #21

fubz opened this issue Sep 11, 2024 · 1 comment

Comments

@fubz
Copy link

fubz commented Sep 11, 2024

I'm trying to use the svelte components; however, they all render as undefined. Here is a minimal example that fails. I have a development relay I'm working with.

<script>
	import { onMount } from 'svelte';
	import { Name, RelayList } from '@nostr-dev-kit/ndk-svelte-components';
	import NDK, { NDKEvent, NDKNip07Signer } from '@nostr-dev-kit/ndk';

    let ndk;
    let npub = "npub1srwzlgp39charftweq5k28xeea4lr02fmwu0jhyhcj9sfdhtkjxstqm070"

	onMount(async () => {
        localStorage.debug = 'ndk:*'

		ndk = new NDK({ explicitRelayUrls: ['wss://relay.nostr-dev.lan'] });
		await ndk.connect();

           
        const dev = await ndk.getUser({npub: npub})
        if(dev) {
            console.log('dev user', dev)
        }
    })

   
</script>

<h1>NDK Svelte Components</h1>
<code>{npub}</code>
<hr>
<div>
    Name: <Name ndk={ndk} npub={npub}/>
</div>
<div>
    <RelayList ndk={ndk}/>
</div>

Console logs:

test:39 [HMR][Svelte] Unrecoverable HMR error in <Root>: next update will trigger a full reload
+page.svelte:18 dev _NDKUser {ndk: NDK, profile: undefined, _npub: 'npub1srwzlgp39charftweq5k28xeea4lr02fmwu0jhyhcj9sfdhtkjxstqm070', _pubkey: undefined, relayUrls: Array(0), …}
@nostr-dev-kit_ndk-svelte-components.js?v=b5da03b9:294275 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'pool')
    at update (@nostr-dev-kit_ndk-svelte-components.js?v=b5da03b9:294275:45)

The html source for the Name component:
<span class="name undefined"></span>

@pablof7z
Copy link
Contributor

you are creating ndk in the onMount call, which will run after rendering the page, so ndk will in fact be undefined on first run; either instantiate ndk outside the onMount or wrap your use in {#if ndk}

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