react-native中的异步存储
我使用console.log和AsyncStorage.getItem函数实现了componentDidMount。我想在获取令牌值后调用axios。react-native中的异步存储,react-native,asyncstorage,React Native,Asyncstorage,我使用console.log和AsyncStorage.getItem函数实现了componentDidMount。我想在获取令牌值后调用axios。 但问题是AsyncStorage.getItem不工作。get//console.log('get')以日志形式打印,不带res. 请问有什么帮助如何获得价值?我做错什么了吗?你可以按如下方式做 componentDidMount(){ console.log("get") AsyncStorage.getItem('token
但问题是AsyncStorage.getItem不工作。get
//console.log('get')
以日志形式打印,不带res.请问有什么帮助如何获得价值?我做错什么了吗?你可以按如下方式做
componentDidMount(){
console.log("get")
AsyncStorage.getItem('token')
.then((res)=>{
console.log(res)
})
}
你可以这样做
componentDidMount(){
console.log("get")
AsyncStorage.getItem('token')
.then((res)=>{
console.log(res)
})
}
尝试重新启动应用程序,异步存储将正常运行 尝试重新启动应用程序,异步存储将正常运行 如果
getItem()
方法返回一个承诺,那么您的代码和他的代码在行为上是相同的。这就是为什么我们使用wait
关键字。这就是为什么您的代码无法解决他的问题。这是相同的功能。我更新了答案,我认为当前令牌值为null或未定义。这是可能的。如果getItem()
方法返回承诺,那么您的代码和HI在行为上都是相同的。这就是为什么我们使用wait
关键字的原因。这就是为什么您的代码无法解决他的问题。它的功能是相同的。我更新了答案,我认为当前的令牌值为null或未定义。这是可能的。我将首先向承诺链添加一个.catch().method,以查看是否有任何错误。@mattdevio我现在添加了.catch(),但其中没有打印错误。我将首先添加一个.catch()方法,以查看是否有任何错误。@mattdevio我现在添加了.catch(),但没有在其中打印错误。