R
R
Rufix2020-03-24 23:17:20
JavaScript
Rufix, 2020-03-24 23:17:20

Adding object to existing React Native AsyncStorage?

Hello. There is the following piece of code:

saveData(id, medicineName) {
        let note = { id, medicineName }
        let noteList = AsyncStorage.getItem('noteList');
        AsyncStorage.setItem('noteList', noteList += JSON.stringify(note));
}


saveData is called when the button is clicked, the principle of operation is similar to the classic one.
Various note objects are added to AsyncStorage with their ID and name, but the problem is that when the content is output to the console, the following appears:
5jtZR2Q.png
That is, with each subsequent addition of a new "note", only the last one is correctly displayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-03-25
@Seasle

saveData(id, medicineName) {
    let note = { id, medicineName }
    let noteList = AsyncStorage.getItem('noteList');
    let parsedNoteList = noteList ? JSON.parse(noteList) : [];
    parsedNoteList.push(note);

    AsyncStorage.setItem('noteList', JSON.stringify(parsedNoteList));
}

As I understand it, there should be a collection, not a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question