Skip to content

Calling mergeItem with a json array crashes the app #699

Open
@zhuangdaz

Description

@zhuangdaz

What happened?

I created an array of users and then tried to update one of the user by passing a new JSON array string. Then the app crashes.

const USER_1 = {
  name: "Tom",
  age: 20,
  traits: {
    hair: "black",
    eyes: "blue",
  },
};

const USER_2 = {
  name: "Sarah",
  age: 21,
  hobby: "cars",
  traits: {
    eyes: "green",
  },
};

const USER_3 = {
  name: "Sarah",
  age: 22,
  hobby: "bike",
  traits: {
    eyes: "green",
  },
};

const USERS = [USER_1, USER_2];

const mergeUsers = async () => {
  try {
    console.log("creating users");
    await AsyncStorage.setItem("@MyApp_users", JSON.stringify(USERS));

    console.log("merging user 3");
    await AsyncStorage.mergeItem("@MyApp_users", JSON.stringify([USER_3]));

    // read merged item
    console.log("reading merged users");
    const currentUsers = await AsyncStorage.getItem("@MyApp_users");
    console.log(currentUsers);
  } catch (err) {}
};

Version

1.15.0

What platforms are you seeing this issue on?

  • Android
  • iOS
  • macOS
  • Windows
  • web

System Information

info Fetching system and libraries information...
System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 473.64 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm
    Watchman: 2021.10.04.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: Not Found
  SDKs:
    iOS SDK: Not Found
    Android SDK: Not Found
  IDEs:
    Android Studio: 1.3 AI-141.2135290
    Xcode: /undefined - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.10 - /Users/zhuangda/.jenv/shims/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: ^17.0.2 => 17.0.2 
    react-native: 0.64.2 => 0.64.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to Reproduce

Described above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions