【发布时间】:2019-06-28 01:06:28
【问题描述】:
我迷路了,我一直在尝试很多事情,我不明白为什么当我尝试从 asyncstorage react native 读取时会出现此错误,我知道我将 asyncstorage 作为字符串获取需要使用 parse 之类的方法对其进行转换,但我陷入了这种错误。
class AddScreen extends React.Component {
constructor(props) {
i=0;
super(props);
var text = {};
this.state = {text: ''};
}
setName = (text) => {
text.name = text;
// alert(t)
var name = ("name"+i++);
// alert(name);
AsyncStorage.setItem(name, text);
this.setState({ name: text });
var booga= AsyncStorage.getItem(name);
// var stringified = JSON.stringify(booga.name);
var booga1 = JSON.parse(booga);
alert(booga1);
}
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<TextInput
style={{height: 100}}
placeholder="Name of expense"
onChangeText={this.setName}
/>
<View style={{width: 50, height: 100}} />
<Text>Add Screen</Text>
</View>
);
}
}
这是我使用 expo 在设备上运行应用时遇到的错误
Unexpected token o in JSON at position 1
Object.AddScreen._this.setName [as onChangeText]
App.js:26:20
Object._onChange
TextInput.js:1252:42
Object.invokeGuardedCallbackImpl
ReactNativeRenderer-dev.js:93:9
invokeGuardedCallback
ReactNativeRenderer-dev.js:318:30
invokeGuardedCallbackAndCatchFirstError
ReactNativeRenderer-dev.js:342:24
executeDispatch
ReactNativeRenderer-dev.js:715:2
executeDispatchesInOrder
ReactNativeRenderer-dev.js:737:4
executeDispatchesAndRelease
ReactNativeRenderer-dev.js:894:4
executeDispatchesAndReleaseTopLevel
ReactNativeRenderer-dev.js:902:9
【问题讨论】:
-
我怀疑您没有等待对存储的异步调用,并且可能试图解析返回的承诺。 facebook.github.io/react-native/docs/asyncstorage
标签: javascript json react-native