我正在尝试使用AsyncStorage获取数据。每当我调用我的动作创建者 requestData 并对传递的数据进行控制时,我得到类似下面的内容。我有两个版本的getItem。在这两个版本中我都获得了属性字段的无用值。属性值应该是可读的
{" FROM日期":" 20160601"" TODATE":" 20160701""属性&#34 ;:{" _40":0," _65":0," _55":空," _72":空},&# 34; URL":" / ABC / ABC / XYZ"}
async getItem(item) {
let response = await AsyncStorage.getItem(item);
let responseJson = await JSON.stringify(response);
return responseJson;
}
async getItem(item) {
try {
const value = AsyncStorage.getItem(item).then((value) => { console.log("inside componentWillMount method call and value is "+value);
this.setState({'assetIdList': value});
}).then(res => {
return res;
});
console.log("----------------------------value--------------------------------------"+value);
return value;
} catch (error) {
// Handle errors here
console.log("error is "+error);
}
}
componentWillMount() {
requestData({
fromDate: '20160601',
toDate: '20160701',
assetId: this.getItem(cmn.settings.property),
url: '/abc/abc/xyz'
});
}
答案 0 :(得分:0)
您将财产作为承诺,您需要解决它。 尝试使用链接。
assetId: this.getItem(cmn.settings.property).then((res) => res)
.catch((error) => null);
答案 1 :(得分:0)
由于AsyncStorage
本质上是异步的,因此您必须等待它返回对象 然后 调用您的requestData
方法;类似于以下内容 -
class MyComponent extends React.Component {
componentWillMount() {
this.retrieveFromStorageAndRequestData();
}
async getItem(item) {
let response = await AsyncStorage.getItem(item);
// don't need await here since JSON.stringify is synchronous
let responseJson = JSON.stringify(response);
return responseJson;
}
async retrieveFromStorageAndRequestData = () => {
let assetId = await getItem(cmn.settings.property);
requestData({
fromDate: '20160601',
toDate: '20160701',
assetId,
url: '/abc/abc/xyz'
}) ;
}
// rest of the component
render() {
// render logic
}
}