Arrays not working
logileifs opened this issue · comments
I've been following the examples and pushing objects into an array with simple-store but when I try to retrieve from it again I only get the last value pushed. It seems like either the get function only returns the last item or the push function overrides the array
@logileifs can you post a code example to show what you're seeing?
Pushing into the array a few items:
colls.map((coll) =>
{
store.push('userCollections', coll)
})
'adding collection', { created: 1501523967214,
creator: 'FG3Mr1ABl6PTBQRYyT3rTtJ253g2',
key: '-KqOnMmTbIIURwIEaPHV',
lastUpdate: 1501524026014,
name: '1',
status: 'private' }
'adding collection', { created: 1501692407666,
creator: 'FG3Mr1ABl6PTBQRYyT3rTtJ253g2',
key: '-KqYpuxnq9f43hqUtWQ9',
lastUpdate: 1501692407666,
name: 'Testa',
status: 'private' }
'adding collection', { created: 1501693357711,
creator: 'FG3Mr1ABl6PTBQRYyT3rTtJ253g2',
key: '-KqYtXuF4_WaPBiqPu55',
lastUpdate: 1501693357711,
name: 'Testb',
status: 'private' }
Retrieving items again later:
store.get('userCollections').then((colls) =>
{
callback(colls)
})
I only get the last item pushed
I figured out I can push a whole array like this:
store.push('userCollections', [{'name': 'collection1'},{'name':'collection2'}])
And then I can successfully push items into it with:
store.push('userCollections': {'name':'collection3'})
The only problem is that when I retrieve the array later using:
store.get('userCollections').then((colls) => { return colls })
I get a doubly nested array like this:
[[ {'name':'collection1'}, {'name':'collection2'}, {'name':'collection3'} ]]
That's a behavior that is easier to work with since I can flatten the array every time I retrieve it but still not the expected/desired behavior.
I'm unable to reproduce with react-native@0.50.3
and react@16.0.0
. Here's the piece of code I'm testing with. Can either of you @ProteanDev @logileifs run this code and see if you get the output below?
import simpleStore from 'react-native-simple-store';
async function tests() {
const key = 'test';
await simpleStore.push(key, 1);
let val = await simpleStore.get(key);
console.log('value 1', val);
await simpleStore.push(key, 2);
val = await simpleStore.get(key);
console.log('value 2', val);
}
tests();
Output
value 1 [1]
value 2 [1, 2]
Closing due to inactivity. If anyone has any further concerns about the .push
functionality please comment here so we can continue the conversation.