Arrays 如何在React Native with condition中从API的数组结果中获取记录?
这是API中JSON文件的结果:Arrays 如何在React Native with condition中从API的数组结果中获取记录?,arrays,json,reactjs,api,react-native,Arrays,Json,Reactjs,Api,React Native,这是API中JSON文件的结果: { "data": [ { "recordid": "8888abc", "accountno": "00-00-00000-00007-00", "tag": "govid", "filename": "gov_id.png", "path": "C:\\MOBILEAPP\\governmentid/gov_id.p
{
"data": [
{
"recordid": "8888abc",
"accountno": "00-00-00000-00007-00",
"tag": "govid",
"filename": "gov_id.png",
"path": "C:\\MOBILEAPP\\governmentid/gov_id.png",
"ext": ".png",
"posted_dt": "12/11/2019 10:38:20 AM"
},
{
"recordid": "8888abc",
"accountno": "00-00-00000-00007-00",
"tag": "compid",
"filename": "compid.jpg",
"path": "C:\\MOBILEAPP\\compid/cid.jpg",",
"ext": ".jpg",
"posted_dt": "12/11/2019 10:38:20 AM"
}
],
"error_message": "Successfully retrieved.",
"is_success": true,
"requested_on": "12/18/2019 2:14:27 PM"
}
我需要将路径中的标记='govid'放入变量中,因为它用于另一个API获取的头中
async getProfilePhotoPath(token) {
//membid is recordid
let membid = await AsyncStorage.getItem(MEMBER_ID);
let resp2 = await fetch("https://api/files",
{
method: 'GET',
headers: {
"Authorization": "Bearer " + token,
"MemberID": membid,
'Content-Type': 'application/json;charset=UTF-8',
},
},
)
.then(resp2 => {
let respImg = resp2.json();
varImagePath = "should contain data.path where tag = 'govid'"
console.log('This is respImg values',respImg)
return respImg;
})
.catch(error => {
alert('Error in resp2 imgpath!' + error);
});
}
async getProfilePhoto() {
let token = await AsyncStorage.getItem(ACCESS_TOKEN);
this.getProfilePhotoPath(token);
let resp = await fetch("https://api/filepathtoimage", {
headers: {
"Authorization": "Bearer " + token,
"ImagePath": varImagePath,
}
})
let respBlob = await resp.blob();
let reader = new FileReader()
reader.readAsDataURL(respBlob)
reader.onload = () => {
this.setState({ imgsrc: reader.result })
}
}
console.log('这是respImg值',respImg)返回:
This is respImg values
Promise {_40: 0, _65: 0, _55: null, _72: null}
_40: 1
_65: 1
_55:
data: Array(2)
0: {recordid: "8888abc", accountno: "00-00-00000-00007-00", tag: "govid", filename: "gov_id.png", path: "C:\\MOBILEAPP\\governmentid/gov_id.png", …}
1: {recordid: "8888abc", accountno: "00-00-00000-00007-00", tag: "compid", filename: "compid.jpg", path: "C:\\MOBILEAPP\\compid/cid.jpg", …}
length: 2
__proto__: Array(0)
error_message: "Successfully retrieved."
is_success: true
requested_on: "12/18/2019 3:10:32 PM"
__proto__: Object
_72: null
__proto__: Object
如何在varImagePath上设置值(本例中应为'C:\MOBILEAPP\governmentid/gov\u id.png')?只需使用过滤器:
let filterString = 'govid';
const result = arr.filter(f => f.tag == filterString);
例如:
让arr=[
{
“记录ID”:“8888abc”,
“账号”:“00-00-00000-00007-00”,
“标签”:“政府ID”,
“文件名”:“gov_id.png”,
“路径”:“C:\\MOBILEAPP\\governmentid/gov\u id.png”,
“ext”:“.png”,
“已发布”:“2019年11月12日上午10:38:20”
},
{
“记录ID”:“8888abc”,
“账号”:“00-00-00000-00007-00”,
“标记”:“compid”,
“文件名”:“compid.jpg”,
“路径”:“C:\\MOBILEAPP\\compid/cid.jpg”,
“分机”:“.jpg”,
“已发布”:“2019年11月12日上午10:38:20”
}
]
让filterString='govid';
const result=arr.filter(f=>f.tag==filterString);
控制台日志(结果)
resp2.json()
返回承诺
.then(resp2 => resp2.json())
.then(jsonObject => {
const data = jsonObject.data;
const record = data.find(item => item.tag === 'govid');
if (record) {
varImagePath = record.path;
}
})
我将其替换为const varImagePath=arr.filter(f=>f.tag=='govid');它在resp2 imgpath中发出错误警报!ReferenceError:arr未定义此已工作,但我无法使getProfilePhoto()读取varImagePath。您应该等待此函数
等待此函数。getProfilePhotoPath(令牌)代码>