Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
undefined when updating svelte store with an array of objects

I'm trying to update svelte store with an array of users fetched from server:

  const fetchUsers = async () => {
    const resp = await fetch(baseUrl + "/users/");
    if (!resp.ok) {
      throw new Error(`HTTP error ${resp.status}`);
      console.log(Error);
    } else {
      users = await resp.json();
      $UsersStore.update((currentState) => {
      return [...currentState, users];
    });
      console.log("users are:", users);
      console.log("$UsersStore is:", $UsersStore);
    }
  };

The store is defined as follows:

import {writable} from 'svelte/store';

const UsersStore = writable ([]);

export default UsersStore;

The console log values are like:

users are: (4) [{…}, {…}, {…}, {…}]
$UserStore is: [Array(4)]

So when I iterate over users

{#each $UersStore as user}
    <div> {user.username} </div>
{/each}

I get undefined.

I also tried things like

UsersStore.update((currentState) => {
        return [currentState.push(users)];

But that did not work either.

I'm wondering how can I fix this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, basically I needed to concatenate the users array to the existing store:

  UsersStore.update((us) => {
    return us.concat(users);
  });

I'm not sure its the idiomatic way to update the svelte store but it works nonetheless.

Hopefully this help others too, because I had not seen any such example involving setting fetched json data to store in existing tutorials.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda