Javascript AsyncStorage.getItem在使用fetch-React Native时返回未定义
我试图使用promise和AsyncStorage在登录组件中设置令牌,但当我检索令牌以在其他组件中使用时,出现错误Javascript AsyncStorage.getItem在使用fetch-React Native时返回未定义,javascript,react-native,async-await,fetch,asyncstorage,Javascript,React Native,Async Await,Fetch,Asyncstorage,我试图使用promise和AsyncStorage在登录组件中设置令牌,但当我检索令牌以在其他组件中使用时,出现错误无法读取未定义的属性“getItem”。我尝试使用Async/Await来等待令牌响应,以便将其保存到存储器中,但我得到了相同的错误。如何正确设置令牌 SignIn.js组件 //Function is bound async signIn() { const data = JSON.stringify({username: this.state.username, passwor
无法读取未定义的属性“getItem”。我尝试使用Async/Await来等待令牌响应,以便将其保存到存储器中,但我得到了相同的错误。如何正确设置令牌
SignIn.js组件
//Function is bound
async signIn() {
const data = JSON.stringify({username: this.state.username, password: this.state.password})
await fetch(`https://somesecretdomain.com:8443/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: data
}).then((response) => response.json()).then(async(responseJson) => {
AsyncStorage.setItem('jwt', responseJson.id_token);
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({routeName: 'Profile'})]
});
this
.props
.navigation
.dispatch(resetAction);
}).catch((error) => {
console.warn(error);
})
};
async fetchData() {
AsyncStorage
.getItem('jwt')
.then((value) => {
this.setState({"TOKEN": value});
})
.done();
console.log(this.state.TOKEN)
const response = await
fetch(`https://somesecretdomain.com:8443/users/getUsers`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'token': this.state.TOKEN
}
})
const json = await response.json()
Profile.js组件
//Function is bound
async signIn() {
const data = JSON.stringify({username: this.state.username, password: this.state.password})
await fetch(`https://somesecretdomain.com:8443/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: data
}).then((response) => response.json()).then(async(responseJson) => {
AsyncStorage.setItem('jwt', responseJson.id_token);
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({routeName: 'Profile'})]
});
this
.props
.navigation
.dispatch(resetAction);
}).catch((error) => {
console.warn(error);
})
};
async fetchData() {
AsyncStorage
.getItem('jwt')
.then((value) => {
this.setState({"TOKEN": value});
})
.done();
console.log(this.state.TOKEN)
const response = await
fetch(`https://somesecretdomain.com:8443/users/getUsers`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'token': this.state.TOKEN
}
})
const json = await response.json()
}
我将Profile.js组件更改为如下,仍然得到相同的错误
import React, {AsyncStorage, localStorage} from 'react';
import {
Text,
View,
Image,
TouchableHighlight,
WebView,
TextInput,
KeyboardAvoidingView,
ScrollView
} from 'react-native';
async fetchData() {
const TOKEN = await AsyncStorage.getItem('jwt');
const response = await
fetch(`https://somedomain.com:8443/users/getUsers`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'token' : TOKEN
},
});
const result = await response.json();
this.setState({data: result})
}
试试这个:
async signIn() {
const data = JSON.stringify({username: this.state.username, password: this.state.password})
const response = await fetch(`https://somesecretdomain.com:8443/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: data
});
const result = await response.json();
await AsyncStorage.setItem('jwt', result.id_token);
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({routeName: 'Profile'})]
});
this
.props
.navigation
.dispatch(resetAction);
});
};
为什么要一起使用fetch和wait?wait解析承诺并返回结果。您应该执行类似于const response=await fetch(…)的操作;const result=wait response.json();等待AsyncStorage.setItem(…,result.id_token)按照您的方式,您实际上并不是在等待所有承诺的结果。嗯…得到相同的错误无法读取已定义的属性getItem
如果错误为“无法读取未定义的属性getItem”,则您没有正确导入AsyncStorage。它是在react native中,不是你说的对。将AsyncStorage更改为从React Native而不是React导入解决了此问题。谢谢这与不推荐从react native导入一样有效<代码>从“@react native community/async storage”导入异步存储代码>