gpt4 book ai didi

react-native - react native AsyncStorage : Push to an array with a key

转载 作者:行者123 更新时间:2023-12-04 05:09:06 26 4
gpt4 key购买 nike

嗨,我在向 AsyncStorage 中的数组添加值时遇到问题。

AsyncStorage.getItem('savedIds', (err, result) => {
const id = '1';
if (result !== null) {
console.log('Data Found', result);
result = JSON.parse(result);
result.push(id);
AsyncStorage.setItem('savedIds', JSON.stringify(result));
} else {
console.log('Data Not Found');
AsyncStorage.setItem('savedIds', id);
}
});

AsyncStorage.getItem('savedIds', (err, result) => {
console.log(result);
});

设置初始 id 后,我收到错误“result.push”不是函数。我需要改变什么来解决这个问题?或者有更优雅的解决方案吗?

最佳答案

AsyncStorage.getItem('savedIds', (err, result) => {
const id = [1];
if (result !== null) {
console.log('Data Found', result);
var newIds = JSON.parse(result).concat(id);
AsyncStorage.setItem('savedIds', JSON.stringify(newIds));
} else {
console.log('Data Not Found');
AsyncStorage.setItem('savedIds', JSON.stringify(id));
}
});

关于react-native - react native AsyncStorage : Push to an array with a key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42672692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com