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

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

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 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!