Arrays 值未存储在阵列AsyncStorage react native中
我正在尝试将值作为数组存储在AsyncStorage中,但当我尝试从数组中获取值时,值不会保存在那里,也不会得到任何结果。如有任何帮助,将不胜感激。请提前感谢,以下是我的代码:Arrays 值未存储在阵列AsyncStorage react native中,arrays,react-native,asyncstorage,Arrays,React Native,Asyncstorage,我正在尝试将值作为数组存储在AsyncStorage中,但当我尝试从数组中获取值时,值不会保存在那里,也不会得到任何结果。如有任何帮助,将不胜感激。请提前感谢,以下是我的代码: let searchString = this.state.inputValue AsyncStorage.getItem('searches', (res) => { var searches if (res === null) { searches = [] }else { sea
let searchString = this.state.inputValue
AsyncStorage.getItem('searches', (res) => {
var searches
if (res === null) {
searches = []
}else {
searches = JSON.parse(res)
}
searches.push({
searchString: searchString
})
AsyncStorage.setItem('searches', JSON.stringify(searches), (res) => {
console.log('====FirstPage====setItem==async==='+res)
})
});
//从异步存储获取值:
AsyncStorage.getItem('searches', (res) => {console.log('===res==='+res)})
请试试这个
AsyncStorage.getItem("searches").then((value) => {
this.setState({"searches": value});
}).done();
也看到
您可以按以下方式存储数据:
AsyncStorage.setItem('searches', JSON.stringify(searchString));
AsyncStorage.getItem('searches', (err, result) => { // err indicating the error
console.log("Storage Data(String):", result); // print is string format
console.log("Storage Data(Object):", JSON.parse(result)); // print is json format
})
为了获取/获取,您可以使用以下操作:
AsyncStorage.setItem('searches', JSON.stringify(searchString));
AsyncStorage.getItem('searches', (err, result) => { // err indicating the error
console.log("Storage Data(String):", result); // print is string format
console.log("Storage Data(Object):", JSON.parse(result)); // print is json format
})
能否显示在AsyncStorage中保存/存储值的代码?是否使用AsyncStorage.getItem()?我已更新了我的问题。输入值现在保存在AsyncStorage中,但在第二次输入时,它会覆盖第一个值,而不会像数组一样追加值。您的意思是,您多次执行AsyncStorage.setItem的“搜索”操作?