未定义为异步运行的SharePoint GET调用

问题描述

我在项目中添加了以下代码,并且希望将'fnAppurl'值传递给fetchData方法

  componentDidMount(){
    this.fnAppUrl = this.getApiUrl();  //receiving undefined. Want to wait till the method returns a value
    this._AppService.fetchData(this.fnAppUrl).then(data => 
    this.setState({Metadata:data}),()=>{console.log()});
  }

public getApiUrl():any{
        let requestUrl = this.props.context.pageContext.web.absoluteUrl.concat("/_api/web/lists/getbytitle('listName')/items?$select=Value&$filter%20Title%20eq%20%27APIURL%27")   
        this.props.context.spHttpClient.get(requestUrl,SPHttpClient.configurations.v1)
            .then((response: SPHttpClientResponse) => {  
                if (response.ok) {  
                    response.json().then((responseJSON) => {  
                        if (responseJSON!=null && responseJSON.value!=null){  
                            return responseJSON.value[0].Value;  
                        }  
                    });  
                }  
            },()=>{console.log()}).then(x=>console.log(x));
    }

但是,fetchData方法接收未定义为GET的异步执行。当我调试时可以看到,它首先命中getApiUrl并返回到调用行并执行下一行。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)