无法读取对象(找不到变量错误)



在下面的代码中,我在React Native中的AsyncStorage中存储了一个对象,但在{personValue}中读取它时遇到问题。我有

找不到变量personValue

错误。你能帮我运行代码吗?

const storeData = async () => {
try {
const newperson = JSON.stringify(person);
await AsyncStorage.setItem("@Key", newperson);
alert(newperson);
} catch (e) {

}
};
const getData = async () => {
try {
const personValue = await AsyncStorage.getItem("@Key");
if (personValue !== null) {

console.log(JSON.parse(personValue));
return JSON.parse(personValue);
}
} catch (error) {

}

在控制台中,我可以在GetData方法中正确地看到对象

我发现了我犯的错误。我分析错了变量。

const getData = async () => {
try {
const personValue = await JSON.parse(AsyncStorage.getItem("@Key"));
if (personValue !== null) {
console.log(personValue);
return personValue;
}
}

最新更新