发送请求到快递服务器

时间:2019-06-19 19:05:00

标签: angular express http post

我的服务中有一个功能可以获取用户的当前位置(纬度/经度)。我试图将纬度和经度发送到服务器,以便可以将其添加到查询中。但是,我的发帖请求似乎无效。我已经包含了我的appInitializer模块,以演示如何调用此函数。

eventServer.js

router.get('/ticketmaster-events', (req, res) => {
/*   adding it to this query */
  axios.get('http://app.ticketmaster.com/discovery/v2/events.json?apikey=hneV7jzq6Uz7IyQC7GtEHz5s2nTsU6Jm&size=200')
  .then(res2 => {
    res.send(res2.data._embedded.events)
  })
  .catch(err => res.send(err))
  });

router.route('/currentLatLong').post((req, res, next) => {
  console.log("hello this test")
  try{
    res.end("response is " + req.body);
    console.log(req);
  }
  catch(err) {
    res.send(err);
  }
});

google.service.ts

export class GoogleService {
  uri = 'http://localhost:4000';
  public currentLat: number;
  public currentLng: number;
  public currentLatLong: any = [];

    getUserLocation() {
    /* locate the User */
    if (navigator.geolocation) {
        console.log('good');
        navigator.geolocation.getCurrentPosition(position => {
        this.currentLat = position.coords.latitude;
        this.currentLng = position.coords.longitude;
        this.data.latitudeSource.next(this.currentLat);
        this.data.longitudeSource.next(this.currentLng);
        const currentLatLong = {
          latitude : this.currentLat,
          longitude: this.currentLng
        };
        console.log(currentLatLong);

        return this.http.post(`${this.uri}/currentLatLong`, JSON.stringify(currentLatLong)/* , {
          headers: new HttpHeaders({'Content-Type': 'application-json'})} */
          ).subscribe(response => {
          console.log('your response is here', JSON.stringify(response));
           },
           error => {
             console.log('error occoured', error);
          });
      });
    } else {
      console.log('bad');
    }
  }
  constructor(private data: DataService, private http: HttpClient) { }
}

appInitializer.module.ts

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { TicketMasterService } from '../../services/tmService/ticket-master.service';
import { GoogleService } from '../../services/googleService/google.service';

    export function fetchTmData(tmService: TicketMasterService) {
  return () => tmService.fetchTmData();
}

    export function getUserLocation(googleService: GoogleService) {
  return () => googleService.getUserLocation();
}

    @NgModule({
    imports: [HttpClientModule],
    providers: [
    TicketMasterService,
    GoogleService,
    { provide: APP_INITIALIZER, useFactory: fetchTmData, deps: [TicketMasterService], multi: true },
    { provide: APP_INITIALIZER, useFactory: getUserLocation, deps:[GoogleService], multi: true }
    ]
    })
    export class AppLoadModule { }

1 个答案:

答案 0 :(得分:0)

我不知道您要面对什么错误,但是如果您按以下方式编辑GoogleService服务,这可能会起作用。

export class GoogleService {
  uri = 'http://localhost:4000';
  public currentLat: number;
  public currentLng: number;
  public currentLatLong: any = [];

  getUserLocation() {
    /* locate the User */
    if (navigator.geolocation) {
        console.log('good');
        navigator.geolocation.getCurrentPosition(position => {
        this.currentLat = position.coords.latitude;
        this.currentLng = position.coords.longitude;
        this.data.latitudeSource.next(this.currentLat);
        this.data.longitudeSource.next(this.currentLng);
        this.currentLatLong[0] = this.currentLat;
        this.currentLatLong[1] = this.currentLng;

       this.http.post(`${this.uri}/currentLatLong`, JSON.stringify(this.currentLatLong), {
          headers: new HttpHeaders({'Content-Type': 'application-json'})}
          ).subscribe(response=>{
           consol.log("your response is here")
           },
           error=>{console.log("error occoured")});

      });
    }
  }
  constructor(private data: DataService, private http: HttpClient) { }
}

您应将http requests设为Observable,然后将对象转换为类似于解决方案的字符串。