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

100
Vistas
React Native Async Storage setItem call fails

I'm writing a simple Async Storage set Item call in React Native to store my store's cart data. But the call fails. I can't find possible reason why it so as there is no mention of such in the documentation. I have added the relevant code. Please help would be appreciated.

const ProductDetailScreen = (props) => {
  const {product} = props.route.params;

  const getCart = async () => {
    return await AsyncStorage.getItem('cart');
  };

  const cart = getCart();

  const addToCart = async () => {
    try {
      await AsyncStorage.setItem('cart', [
        ...cart, JSON.stringify(product),
      ]);
    } catch (e) {
      console.error(`Failed to add item: {${JSON.stringify(product)}} to cart`);
    }
  };

  return (
    <>
      <TouchableOpacity onPress={addToCart}>
          <Button>
            <Text style={styles.btnText}>Add to Cart</Text>
          </Button>
        </TouchableOpacity>
    </>
  )
};

export default ProductDetailScreen;

error log

ERROR  Failed to add item: {{"id":2,"name":"Scene Stealers","description":"Corset cuts","price":250,"image":5,"colors":["#0F140D","#DD8560","#E1E0DB"],"sizes":["S","M","L"]}} to cart
almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can only store/set string data in AsyncStorage while you are trying to store Array

enter image description here

await AsyncStorage.setItem('cart', JSON.stringify([...cart, JSON.stringify(product)]))
almost 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