React Native AsyncStorage getItem 返回承诺而不是价值

编程入门 行业动态 更新时间:2024-10-24 04:49:10
本文介绍了React Native AsyncStorage getItem 返回承诺而不是价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个登录表单,我可以发布表单值.在成功的 POST 请求之后,我得到了从 API 返回的认证令牌.我需要将此令牌保存在本地存储中以备将来参考.为了保存这个身份验证令牌,我使用了 AsyncStorage.我使用了 AsyncStorage.setItem(STORAGE_KEY, responseData.auth_token); setItem 方法来保存数据.

I have a login form and I am able to post the form values. After the successful POST request, I get authentication token returned from the API. I need to save this token for future reference in local storage. For saving this auth token I am using AsyncStorage. I used AsyncStorage.setItem(STORAGE_KEY, responseData.auth_token); setItem method to save the data.

如果我通过以下方式控制台记录:

If I console log this by :

console.log(AsyncStorage.setItem(STORAGE_KEY));

它作为这样的承诺对象返回

it returns as promise object like this

Promise {_45: 0, _81: 0, _65: null, _54: null} _45 : 0 _54 : null _65 : "efcc06f00eeec0b529b8" _81 : 1 __proto__

:对象

如何从 AsyncStorage.getItem 方法中获取准确的值?

how can I get the exact value from the AsyncStorage.getItem method?

这是我的获取方法

fetch('54.255.201.241/drivers', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ first_name: this.state.firstName, mobile_number: this.state.mobileNumber, vehicle_number: this.state.vehicleNumber, vehicle_type_id: 1, }) }).then((response) => response.json()).then((responseData) => { if (JSON.stringify(responseData.mobile_number) == (this.state.mobileNumber)) { AsyncStorage.setItem(STORAGE_KEY, responseData.auth_token); console.log(AsyncStorage.getItem(STORAGE_KEY)); this.props.navigator.push({id: 'Otp'}) } else { Alert.alert("SignUp Failed","Mobile number already taken.") } }) .done();

顺便说一句,他们在文档中使用了 await.我尝试使用它,但页面没有加载.这里附有截图.

BTW in documentation they have used await. I tried using that but the pages are not loading.Here with attached the screenshot.

推荐答案

使用 Async/Await :

using Async/Await :

async _getStorageValue(){ var value = await AsyncStorage.getItem('ITEM_NAME') return value }

更多推荐

React Native AsyncStorage getItem 返回承诺而不是价值

本文发布于:2023-10-25 08:12:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   价值   Native   React   getItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!