Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

300
Vistas
Svelte - Trying to access a 'readable' object within 'store.js' returns 'undefined'

As you can see in the image below, you can access a normal Object (which is to be expected) but not a readable one inside stores.js.

App.svelte

<script>
    import { myStuff, myStuff2 } from './stores.js';
</script>

<h1>This is {$myStuff.TEST}</h1>
<h1>This is {myStuff2.TEST}</h1>

stores.js

import { readable } from 'svelte/store';

export const myStuff = readable({
    TEST: "HELLO"
});

export const myStuff2 = {
    TEST: "HELLO 2"
};

// Doesn't work
console.log("TEST FROM STORE: " + myStuff.TEST);

// Works
console.log("TEST FROM STORE: " + myStuff2.TEST);

script_result

Why does this happen?

The Tutorial mentions briefly that you might get an undefiend but it doesn't elaborate much past that and the Docs don't mention that possible outcome as of writing this.

Screenshot of the example from the marked answer:

use_within_storejs_example

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The way stores work in Svelte is that they wrap your value in a class which doesn't have a direct way to get the stored value. You would need to subscribe to it to get it. There's also a get method which does the same under the hood.

So in your code this would print the value. It will also print the new values as it changes.

const unsub=mystuff.subscribe(val=> console.log("CURRENT VALUE OF TEST FROM STORE: " + val.TEST))
unsub()

//this does the same thing:
import { get } from 'svelte/store';
console.log("TEST FROM STORE: " + get(myStuff).TEST);

In the .svelte files the $____ notation does this subscription for you to make it more convenient to use it.

about 4 years ago · Santiago Trujillo Denunciar

0

You can use get:

Generally, you should read the value of a store by subscribing to it and using the value as it changes over time. Occasionally, you may need to retrieve the value of a store to which you're not subscribed. get allows you to do so.

For example:

import { readable, get } from 'svelte/store';

console.log("TEST FROM STORE: " + get(myStuff).TEST);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda