Javascript 角度http post错误:无法读取属性';邮政';未定义的

Javascript 角度http post错误:无法读取属性';邮政';未定义的,javascript,angular,typescript,http,dependency-injection,Javascript,Angular,Typescript,Http,Dependency Injection,我正在尝试发出我的第一个http POST请求。我的GET请求工作正常(在相同的服务中),但是当我尝试发出POST请求时,我得到了错误 错误类型错误:无法读取未定义的属性“post” 在 ApiService.push../src/app/services/api.service.ts.ApiService.getTracsStartEvents (api.service.ts:57) 在同一个文件中,我使用它的方式与使用GET请求的方式相同。我不明白为什么POST请求不起作用。我一定是语法上有

我正在尝试发出我的第一个http POST请求。我的GET请求工作正常(在相同的服务中),但是当我尝试发出POST请求时,我得到了错误

错误类型错误:无法读取未定义的属性“post” 在 ApiService.push../src/app/services/api.service.ts.ApiService.getTracsStartEvents (api.service.ts:57)

在同一个文件中,我使用它的方式与使用GET请求的方式相同。我不明白为什么POST请求不起作用。我一定是语法上有问题。谁能给我指出正确的方向吗

import { Device } from '../shared/device';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class ApiService {
  TRACS_URL = '<REMOVED>';
  DELORME_LOCATE_URL = '<REMOVED>';

  apiKey = '<REMOVED>';
  getAllDeviceAPI = 'PApps_AircraftInfo';
  getDeviceByIMEIAPI = 'PApps_AircraftInfo/FindByIMEI/';
  getDeviceByTailNumberAPI = 'PApps_AircraftInfo/FindByTailNumber/';
  getDeviceByCallsignAPI = 'PApps_AircraftInfo/FindByCallsign/';
  getTracsStartEventsAPI = 'GetStartTrackEvents';

  constructor(private httpClient: HttpClient) {}

  public createDevice( device: Device ){}

  public updateDevice( device: Device ) {
    console.log('going to call API to update device: ', device)
  }

  public deleteDevice( device: Device ) {
    console.log('going to call API to delete device: ', device);
  }

  public getDeviceByIMEI( imei: string ) {
    return this.httpClient.get<Device[]>(`${this.TRACS_URL}/${this.getDeviceByIMEIAPI}/${imei}?apikey=${this.apiKey}`);
  }

  public getDeviceByTailNumber( tailNumber: string ) {
    return this.httpClient.get<Device[]>(`${this.TRACS_URL}/${this.getDeviceByTailNumberAPI}/${tailNumber}?apikey=${this.apiKey}`);
  }
  public getDeviceByCallsign( callsign: string ) {
    return this.httpClient.get<Device[]>(`${this.TRACS_URL}/${this.getDeviceByCallsignAPI}/${callsign}?    apikey=${this.apiKey}`);
  }
  public getAllDevices( url?: string ) {
    return this.httpClient.get<Device[]>(`${this.TRACS_URL}/${this.getAllDeviceAPI}?apikey=${this.apiKey}`);
  }

  public getTracsStartEvents( imeiList: string[] ) {
    console.log('imeiList: ', imeiList );
    const httpHeaders = new HttpHeaders({
        'x-api-key': 'Ra4GyPWuzU1PKDKdmHyyK4WlMKV7v3j4JQhaU7i8',
        'Content-Type': 'application/json-patch+json',
        'Cache-Control': 'no-cache',
    });
    let options = {
      headers: httpHeaders
    };
    return this.httpClient.post<any[]>    (`${this.DELORME_LOCATE_URL}/${this.getTracsStartEvents}`,
      {
        data: { arr_imei: imeiList,
                searchType: 'REALTIME',
              }
      }, options ).subscribe( res => {
        console.log('query result:', res );
      });
    }
}
在my
HomeComponent
中,一切都从何处开始:

export class HomeComponent implements OnInit, OnChanges {
   constructor(public appSettingsService: AppSettingsService,
               public layerControlDialogComponent: MatDialog,
               private devicesToTrackService: DevicesToTrackService,
               private positionUpdateService: PositionUpdateService,
               private httpClient: HttpClient) { }
  startTracking(devices) {
     console.log('going to start tracking ', devices);
     this.positionUpdateService.getMissionStartEvents(devices)
  }

确保将您的
ApiService
注释为
Injectable()

然后,不要自己创建
ApiService
,这就是依赖注入的目的。它将自动创建带有其依赖项的
ApiService
实例(
HttpClient
):


确保将您的
ApiService
注释为
Injectable()

然后,不要自己创建
ApiService
,这就是依赖注入的目的。它将自动创建带有其依赖项的
ApiService
实例(
HttpClient
):


首先,http.get和http.post return observables,除非您订阅,否则wish不会执行,如下所示:

this.apiService.getTracsStartEvents(devices).subscribe()
其次,Angular中的依赖项注入不是这样工作的

  • 不要使用new关键字来实例化apiService,直接使用它就像使用httpClient一样,只需使用this.apiService

  • 最后,确保您已经提供了apiService,如果没有,请填写以下内容:

    @Injectable({providedIn: ‘root’})
    export class ApiService { ...
    

  • 而且get方法也不应该起作用。如果您尝试订阅它,您将有相同的错误,可能是因为构造函数中的apiService实例化

    首先,http.get和http.post返回可观测值,除非您订阅,否则wish不会执行,如下所示:

    this.apiService.getTracsStartEvents(devices).subscribe()
    
    其次,Angular中的依赖项注入不是这样工作的

  • 不要使用new关键字来实例化apiService,直接使用它就像使用httpClient一样,只需使用this.apiService

  • 最后,确保您已经提供了apiService,如果没有,请填写以下内容:

    @Injectable({providedIn: ‘root’})
    export class ApiService { ...
    


  • 而且get方法也不应该起作用。如果您尝试订阅它,您将有相同的错误,可能是因为构造函数中的apiService实例化

    如何调用
    getTracssStartEvents
    ?我看到的唯一一行
    post
    返回this.httpClient.post
    ,因此逻辑告诉我
    this.httpClient
    未定义。检查您的代码或尝试在stackblitz.com上复制它,以便我们可以检查问题。但是我在上面的函数中使用了httpClient。这就是为什么它会在两行之后取消定义的原因,因为
    这个
    引用取决于调用函数的方式。你必须使用箭头函数而不是匿名函数(至少这是我的假设)。请在调用
    getTracsStartEvents
    如何调用
    getTracsStartEvents
    的地方添加代码段。我看到的
    post
    中唯一一行是
    返回this.httpClient.post
    ,因此逻辑告诉我
    this.httpClient
    未定义。检查您的代码或尝试在stackblitz.com上复制它,以便我们可以检查问题。但是我在上面的函数中使用了httpClient。这就是为什么它会在两行之后取消定义的原因,因为
    这个
    引用取决于调用函数的方式。你必须使用箭头函数而不是匿名函数(至少这是我的假设)。请在调用
    getTracssStartEvents
    的地方添加代码段,它是可注入的。但是现在当我摆脱对象的创建时,我得到了一个不同的未定义错误-现在它返回错误类型错误:无法使用
    provideIn:'root'
    ?如何实例化
    位置更新服务
    ?它还必须由依赖注入创建。这是一个神奇的按钮!我在home组件中手动实例化UpdateService,这就是问题的根源!非常感谢。很高兴它现在对您有效。:-)我建议阅读它是可注射的。但是现在当我摆脱对象的创建时,我得到了一个不同的未定义错误-现在它返回错误类型错误:无法使用
    provideIn:'root'
    ?如何实例化
    位置更新服务
    ?它还必须由依赖注入创建。这是一个神奇的按钮!我在home组件中手动实例化UpdateService,这就是问题的根源!非常感谢。很高兴它现在对您有效。:-)我推荐阅读
    @Injectable({providedIn: ‘root’})
    export class ApiService { ...