Javascript 从一个服务到另一个服务的角度6传递值?

Javascript 从一个服务到另一个服务的角度6传递值?,javascript,angular,typescript,service,Javascript,Angular,Typescript,Service,我试图在我的列表服务中使用地理位置服务中的纬度和经度。不幸的是,它一直返回为未定义。我真的不确定问题出在哪里 list.service.ts import { Injectable } from '@angular/core'; import { Http, Headers } from '@angular/http'; import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent,

我试图在我的列表服务中使用地理位置服务中的纬度和经度。不幸的是,它一直返回为未定义。我真的不确定问题出在哪里

list.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, SubscriptionLike, PartialObserver } from 'rxjs';
import { List } from '../models/list.model';
import { map } from 'rxjs/operators';
import { GeolocationService } from '../services/geolocation.service';

@Injectable()
export class ListService {

    constructor(private http: Http, private geoServ: GeolocationService) { }

    getLongitude() {
        this.geoServ.getLongitude().subscribe((longitude) => {
            console.log(longitude)
        });
    }

    private serverApi = 'http://localhost:3000';



    public getAllLists(): Observable<List[]> {

        const URI = `${this.serverApi}/yelp/${longitude}/${latitude}`;

        return this.http.get(URI)
            .pipe(map(res => res.json()))
            .pipe(map(res => <List[]>res.businesses));


    }
}

谢谢你花时间看这个。我非常感谢您对我下面补充问题的进一步帮助。在我添加更多详细信息之前,文本不允许我发布。

您不会从方法返回任何内容。您应该使用
return
语句。您还需要使用回调函数、承诺函数或可观察函数。我会给你一个明显的例子:

getLongitude() {
  this.geoServ.getLongitude().subscribe((longitude) => {
    console.log(longitude)
  });
}
并更改您的
获取经度

getLongitude() {
  return new Observable((observer) => {
    console.log('Geolocation working!');
    const options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    const success = (pos) => {
      const crd = pos.coords;
      console.log(`Longitude : ${crd.longitude}`);
      const longitude = crd.longitude;
      observer.next(longitude);
      observer.complete();
    };
    const error = (err) => {
      console.warn(`ERROR(${err.code}): ${err.message}`);
      observer.error(err);
      observer.complete();
    };
    navigator.geolocation.getCurrentPosition(success, error, options);
  });  
}
如果要在getAllList方法中获取经度和纬度,可以使用
mergeMap
操作符:

public getAllLists(): Observable<List[]> {
  return this.geoServ.getGeoLocation().pipe(
    mergeMap(({ latitude, longitude}) => 
      this.http.get(`${this.serverApi}/yelp/${longitude}/${latitude}`)
    ),
    map(res => res.businesses)
  );
}
public GetAllList():可观察{
返回此.geoServ.getGeoLocation()管道(
合并地图({纬度,经度})=>
this.http.get(`this.serverApi}/yelp/${longitude}/${latitude}`)
),
地图(res=>res.business)
);
}

我在这里使用的是
HttpClient
。这是angular的“新”http模块。建议用这个代替旧的。它还自动为您执行
json()
调用。

我认为您在GeolocationService中的语法是错误的。你需要订阅getCurrentPositions的Observable,不是吗?“pos”应该从哪里来?我不确定这就是问题所在,是吗?我能够执行:console.log(
Latitude:${crd.Latitude}
);log(
经度:${crd.Longitude}
);在控制台上看到了吗?谢谢!我会投票的,但我不能,直到我得到5个以上的声誉。如果你不介意的话,还有一个问题?如果我试图在列表服务URI中包含这些值(我将其余代码添加到上面的list.service.ts中)@jaimomencayo,我已经更新了我的答案,使之符合我认为你的意思。请确保更改
getGeoLocation
以返回包含纬度和经度的对象的可观察对象,否则代码中会出现一个小问题,认为您可以解决这个问题。我提出了一个新问题,这样你就可以获得另一个问题的学分
public getAllLists(): Observable<List[]> {
  return this.geoServ.getGeoLocation().pipe(
    mergeMap(({ latitude, longitude}) => 
      this.http.get(`${this.serverApi}/yelp/${longitude}/${latitude}`)
    ),
    map(res => res.businesses)
  );
}