基本上,在第一个调用中,我想从多值查找字段中获取项目的ID。然后,我想使用foreach中的ID为每个ID调用REST API,以获取更多详细信息。但是,也许有完全不同的方法,因此,如果此方法不合适,请纠正我。
当前代码:
private _getAssignments(itemID: number)
{
this.readAssignmentIDs(itemID);
}
private readIDs(itemID: number) {
this.props.spHttpClient.get(`${this.props.siteUrl}/_api/web/lists/GetByTitle('LISTNAME')/items(`+itemID+`)?$select=Assignments/ID&$expand=Assignments`,
SPHttpClient.configurations.v1, { headers: { 'Accept': 'application/json;odata=nometadata', 'odata-version': '' }
}).then((response: SPHttpClientResponse): Promise<{value: any[]}> =>{
return response.json();
}).then((response: {value: any[]}): void => {
// save the collection of IDs for later use
this.setState({
dialogItemAssignmentsIDs: response[' Assignments ']
});
// call to another method to use the collection of IDs for the FOREACH
this.readRecords(response[' Assignments ']);
}, (error: any): void => { // handle error
});
}
private readRecords(items: any[]) {
this.state.dialogItemAssignmentsIDs.forEach(element => {
this.props.spHttpClient.get(`${this.props.siteUrl}/_api/web/lists/GetByTitle('Assignments')/items(`+element.ID+`)?$select=OwnerPerson/Name,AssignedToPerson/Name&$expand=OwnerPerson,AssignedToPerson`,
SPHttpClient.configurations.v1, { headers: { 'Accept': 'application/json;odata=nometadata', 'odata-version': '' }
}).then((response: SPHttpClientResponse): Promise<{value: any[]}> =>{
return response.json();
}).then((response: {value: any[]}): void => {
this.setState({
dialogItemAssignmentsRecords: response.value
});
});
});
}
我不知道如何编写一个异步函数来返回数据,然后在第一个方法完成时依次调用另一个方法。