You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中如何正确使用AsyncStorage multiGet获取已存储数据?

Hey there! Let's work through this AsyncStorage multiGet issue step by step—you're already halfway there since your multiSet is working perfectly.

First, Let's Clarify the Correct keys Format for multiGet

The biggest gotcha here is mixing up the parameter formats between multiSet and multiGet:

  • multiSet expects a 2D array (each entry is a [key, value] pair)
  • multiGet only needs a 1D array of string keys—no nested arrays, just the exact key names you stored.

For your specific case, since you stored @BarcodeList and @ScannedBarcode, your keys array should be:

const keys = ['@BarcodeList', '@ScannedBarcode'];

Working multiGet Examples

Here are two common implementation styles that should work for your app:

1. Callback Style (Matching the Docs Example)

const keys = ['@BarcodeList', '@ScannedBarcode'];

AsyncStorage.multiGet(keys, (err, stores) => {
  if (err) {
    console.error('Failed to fetch data:', err);
    return;
  }

  // `stores` is a 2D array where each entry is [key, value]
  stores.forEach(([key, value]) => {
    console.log(`Key: ${key}, Raw Value: ${value}`);
    
    // Don't forget to parse the JSON for your barcode list!
    if (key === '@BarcodeList' && value) {
      const parsedBarcodeList = JSON.parse(value);
      console.log('Parsed Barcode List:', parsedBarcodeList);
    }
  });
});

2. Async/Await Style (Cleaner & More Modern)

This is my preferred approach since it avoids callback nesting:

try {
  const keys = ['@BarcodeList', '@ScannedBarcode'];
  const stores = await AsyncStorage.multiGet(keys);

  stores.forEach(([key, value]) => {
    console.log(`Key: ${key}, Raw Value: ${value}`);
    
    if (key === '@BarcodeList' && value) {
      const parsedBarcodeList = JSON.parse(value);
      console.log('Parsed Barcode List:', parsedBarcodeList);
    }
  });
} catch (err) {
  console.error('Failed to fetch data:', err);
}

Troubleshooting Steps If It's Still Not Working

If you're still hitting issues, run through these checks:

  • Double-check key spelling & case: AsyncStorage keys are case-sensitive. Make sure @BarcodeList in multiGet matches exactly what you used in multiSet (no typos like @barcodeList).
  • Verify keys exist in storage: Use getAllKeys() to confirm your target keys are actually stored:
    const allStoredKeys = await AsyncStorage.getAllKeys();
    console.log('All stored keys:', allStoredKeys);
    
  • Handle null/empty values: If a key doesn't exist, value will be null. Add a check before parsing JSON to avoid errors:
    if (key === '@BarcodeList' && value !== null) {
      const parsedList = JSON.parse(value);
      // Use the parsed data
    } else {
      console.log(`Key ${key} has no value stored`);
    }
    
  • Check for error details: Always log the err object in callbacks or catch blocks—it will tell you exactly what's wrong (e.g., permission issues, invalid key formats).

内容的提问来源于stack exchange,提问作者Nerius Jok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:35