Javascript Spfx React TypeError:无法读取属性';道具';在makeRequest(httpClient.post上下文)中未定义的

Javascript Spfx React TypeError:无法读取属性';道具';在makeRequest(httpClient.post上下文)中未定义的,javascript,reactjs,typescript,react-hooks,spfx,Javascript,Reactjs,Typescript,React Hooks,Spfx,我是React新手,我正在尝试在React组件的spfx中使用httpClient.post调用api,但我遇到了下一个错误:Uncaught(in promise)TypeError:无法读取未定义的属性“props” 应要求。我尝试过使用this.props、this.context,在方法外部设置$this,并将函数makeRequest设置为箭头函数,但似乎没有任何方法能够获得上下文。该函数在my.tsx文件中如下所示: function makeRequest(): Promi

我是React新手,我正在尝试在React组件的spfx中使用httpClient.post调用api,但我遇到了下一个错误:Uncaught(in promise)TypeError:无法读取未定义的属性“props” 应要求。我尝试过使用this.props、this.context,在方法外部设置$this,并将函数makeRequest设置为箭头函数,但似乎没有任何方法能够获得上下文。该函数在my.tsx文件中如下所示:

    function makeRequest(): Promise<HttpClientResponse>  {

  {...}

  const httpClientOptions: IHttpClientOptions = {
    body: body,
    headers: requestHeaders
  };
}
  return this.props.context.httpClient.post(
    postURL,
    HttpClient.configurations.v1,
    httpClientOptions)
    .then((response: Response): Promise<HttpClientResponse> =>  {
      console.log("REST API response received.");
      console.log(response.json);
      return response.json();
    });
}
makeRequest();
函数makeRequest():Promise{
{...}
常量httpClientOptions:IHttpClient={
身体:身体,,
标题:请求标题
};
}
返回this.props.context.httpClient.post(
姿势,
HttpClient.configurations.v1,
httpClientOptions)
.然后((回应:回应):承诺=>{
log(“收到RESTAPI响应”);
log(response.json);
返回response.json();
});
}
makeRequest();
在.ts中:

    import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'SpfxWebPartWebPartStrings';
import { HttpClient, IHttpClientOptions, HttpClientResponse } from '@microsoft/sp-http';
import { ISpfxWebPartProps } from './components/ISpfxWebPartProps';
import {
  IDynamicDataSource
} from '@microsoft/sp-dynamic-data';
import { WebPartContext } from '@microsoft/sp-webpart-base';
export interface ISpfxWebPartWebPartProps {
  description: string;
  context: any;

}

export default class SpfxWebPartWebPart extends BaseClientSideWebPart<ISpfxWebPartWebPartProps> {

  public render(): void {
    
    const element: React.ReactElement<ISpfxWebPartProps> = React.createElement(
      SpfxWebPart,
      {
        description: this.properties.description,
        context : this.context,
      }
    );

    ReactDom.render(element, this.domElement);
  }
import*as React from'React';
从“react dom”导入*作为react dom;
从'@microsoft/sp core library'导入{Version};
进口{
IPropertyPaneConfiguration,
PropertyPaneTextField
}从“@microsoft/sp属性窗格”;
从'@microsoft/sp webpart base'导入{BaseClientSideWebPart};
从“SPFXWebPartWebPartString”导入*作为字符串;
从“@microsoft/sp http”导入{HttpClient,ihttppclientions,HttpClientResponse};
从“/components/ISpfxWebPartProps”导入{ISpfxWebPartProps};
进口{
IDynamicDataSource
}来自“@microsoft/sp动态数据”;
从'@microsoft/sp webpart base'导入{WebPartContext};
导出接口ISpfxWebPartWebPartProps{
描述:字符串;
背景:任何;
}
导出默认类SpfxWebPartWebPart扩展BaseClientSideWebPart{
公共呈现():void{
常量元素:React.ReactElement=React.createElement(
SPFXWeb部件,
{
description:this.properties.description,
context:this.context,
}
);
render(元素,this.domeElement);
}

非常感谢您的帮助。

我最终使用了axios库,因为我看到httpClient出现了很多问题。