Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!