Skip to content

Cant assign remote function query result to $state #14967

@Rafterman1217

Description

@Rafterman1217

Describe the bug

let messages : Message[] = $state(await getMessages(untrack(() => messageFilter)));

triggers Uncaught (in promise) TypeError: resource.then is not a function, but only when its triggered via preload, i can properly use the page by just doing a refresh or navigating to the url directly

Reproduction

just use $state and assign it a remote function result like I did.

let messages : Message[] = $state(await getMessages(untrack(() => messageFilter)));

I use transport hooks if this matters

       static encode(value: Message): any {
          return value instanceof Message && {
              id: value.id,
              from: value.from,
              content: value.content,
              status: value.status,
              created_at: value.createdAt
          };
      }
  
      static decode(data: any): Message | null {
          return data && new Message(data);
      }

unfortunately I cant link the repo.

Why not using $derived?

I want to do update on scroll and therefore i need to manipulate the message array by myself. When I use derived I throw away the results I've already fetched.

const loadMore = async () => {
		if (isLoading || !hasMore) return;
		
		isLoading = true;
		messageFilter.offset += messageFilter.limit;
		
		try {
			const newMessages = await getMessages(messageFilter);
			
			if (newMessages.length < messageFilter.limit) {
				hasMore = false;
			}
			
			if (newMessages.length > 0) {
				messages.push(...newMessages);
			} else {
				hasMore = false;
			}
		} catch (error) {
			console.error('Failed to load more messages:', error);
			toastStore.addMessage({ 
				type: 'error', 
				message: 'Failed to load more messages' 
			});
		} finally {
			isLoading = false;
		}
	};

This has worked fine until my last run of pnpm update

Logs

Uncaught (in promise) TypeError: resource.then is not a function

System Info

System:
    OS: Linux 6.6 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (16) x64 AMD Ryzen 7 5700G with Radeon Graphics
    Memory: 5.72 GB / 15.29 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 25.2.0 - /usr/local/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 11.6.2 - /usr/local/bin/npm
    pnpm: 10.22.0 - /usr/local/bin/pnpm

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions