如何解决执行方法序列-React
我编写了以下代码来获取当前用户组 Ids ,后来从该 Ids 中我希望从SharePoint列表中筛选数据。首先我要执行
ServiceManager.getRemoteService()。getCurrentUserGroups()
方法。获得组 Ids 后,我要执行下一个方法。但是遗憾的是,REST查询的过滤器部分为空,因为
this.groupIdString
为空白。这可能是因为两种方法都同时执行。我是React的新手,我想知道有什么最好的方法来管理这种情况。基本上我想按我写的顺序执行这些方法。
public componentDidMount() {
this.groupIdString= "";
ServiceManager.getRemoteService().getCurrentUserGroups(this.props.siteUrl,"/_api/web/currentuser/groups").then((value) => {
if (value[0]) {
for (let i: number = 0; i < value.length; i++) {
if(value[i].Id != undefined){
this.groupIdString += "(UserGroupsId eq " + value[i].Id.toString()+")";
}
}
}
});
const restQuery = "/_api/Web/Lists/GetByTitle('Weather')/Items?$select=Title,NewsBody,UserGroupsId&$filter=("+this.groupIdString+")";
ServiceManager.getRemoteService().getWeatherListItems(this.props.siteUrl,restQuery).then((value) => {
if (value[0]) {
//code
}
});
}
解决方法
@Sivakumar Piratheeban,
您可以将第二个请求放在第一个呼叫的回调中。
public componentDidMount() {
this.groupIdString = "";
ServiceManager.getRemoteService().getCurrentUserGroups(this.props.siteUrl,"/_api/web/currentuser/groups").then((value) => {
if (value[0]) {
for (let i: number = 0; i < value.length; i++) {
if (value[i].Id != undefined) {
this.groupIdString += "(UserGroupsId eq " + value[i].Id.toString() + ")";
}
}
// Second
const restQuery = "/_api/Web/Lists/GetByTitle('Weather')/Items?$select=Title,NewsBody,UserGroupsId&$filter=(" + this.groupIdString + ")";
ServiceManager.getRemoteService().getWeatherListItems(this.props.siteUrl,restQuery).then((value) => {
if (value[0]) {
//code
}
});
//
}
});
}
BR
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。