Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2观测值_Javascript_Http_Promise_Angular_Observable - Fatal编程技术网

Javascript 角度2观测值

Javascript 角度2观测值,javascript,http,promise,angular,observable,Javascript,Http,Promise,Angular,Observable,我正在构建一个从facebook获取一些事件的应用程序,请看: 事件组件: events: Object[] = []; constructor(private eventService: EventService) { this.eventService.getAll() .subscribe(events => this.events = events) } 事件服务: getAll() { const accessToken = 'xxxxxxxxxxx';

我正在构建一个从facebook获取一些事件的应用程序,请看:

事件组件:

events: Object[] = [];

constructor(private eventService: EventService) {
  this.eventService.getAll()
    .subscribe(events => this.events = events)
}
事件服务:

getAll() {
  const accessToken = 'xxxxxxxxxxx';
  const batch = [{...},{...},{...},...];
  const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;

  return this.http.post('https://graph.facebook.com', body)
    .retry(3)
    .map(response => response.json())
}
身份验证服务:

getAccessToken() {
    return new Promise((resolve: (response: any) => void, reject: (error: any) => void) => {
      facebookConnectPlugin.getAccessToken(
        token => resolve(token),
        error => reject(error)
      );
    });
  }
我有几个问题:

1) 如何设置每60秒更新事件的间隔

2)
accessToken
的价值实际上来自一个承诺,我应该这样做吗

getAll() {
  const batch = [{...},{...},{...},...];
  this.authenticationService.getAccessToken().then(
    accessToken => {
      const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
      return this.http.post('https://graph.facebook.com', body)
        .retry(3)
        .map(response => response.json())
    },
    error => {}
  );
}
return this.http.post('https://graph.facebook.com', body)
  .retry(3)
  .map(response => response.json())
  .map(response => {
    const events: Object[] = [];
    // Manipulate response and push to events...
    return events;
  })
3) 如果是,我如何处理来自
getAccessToken()
承诺的错误,因为我只返回观察者

4) 来自post请求的响应在默认情况下不会返回对象数组,我必须进行一些操作。我应该这样做吗

getAll() {
  const batch = [{...},{...},{...},...];
  this.authenticationService.getAccessToken().then(
    accessToken => {
      const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
      return this.http.post('https://graph.facebook.com', body)
        .retry(3)
        .map(response => response.json())
    },
    error => {}
  );
}
return this.http.post('https://graph.facebook.com', body)
  .retry(3)
  .map(response => response.json())
  .map(response => {
    const events: Object[] = [];
    // Manipulate response and push to events...
    return events;
  })
  • 将事件放入超时块中,并将间隔设置为60秒<代码>设置超时(()=>{},60000)
  • 使用模板字符串是完全正确的,但是您可以告诉我们它的值来自于一个承诺。若您的整个代码块都在promise的resolve函数中,那个么这应该没问题。所以这取决于你的代码在哪里。为什么是承诺。在A2中,建议使用可观察的,而不是承诺。不要把它们混在一起
  • 您没有在error函数中返回任何内容。所以,如果您从该块返回错误,您将在发生错误时获得错误数据
    error=>error
    error=>{returnerror;}
  • 确切地说,您应该映射以获取响应并对其进行操作,然后仅从该函数返回数组
    .map(resp=>{return resp.array})
    。因为respons是JSON格式的,所以现在您必须从中获取数组并返回它。您可以在返回之前进行任意修改
    请随意编辑答案…

    以下是您问题的答案:

    1) 您可以利用观测值的
    间隔
    功能:

    getAll() {
      const accessToken = 'xxxxxxxxxxx';
      const batch = [{...},{...},{...},...];
      const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
    
      return Observable.interval(60000).flatMap(() => {
        return this.http.post('https://graph.facebook.com', body)
                        .retry(3)
                        .map(response => response.json());
      });
    }
    
    getAll() {
      const batch = [{...},{...},{...},...];
      return Observable.fromPromise(this.authenticationService.getAccessToken())
                       .flatMap(accessToken => {
        const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
        return this.http.post('https://graph.facebook.com', body)
                        .retry(3)
                        .map(response => response.json())
        });
    }
    
    2) 您可以在此级别利用可观察对象的
    fromPromise
    功能:

    getAll() {
      const accessToken = 'xxxxxxxxxxx';
      const batch = [{...},{...},{...},...];
      const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
    
      return Observable.interval(60000).flatMap(() => {
        return this.http.post('https://graph.facebook.com', body)
                        .retry(3)
                        .map(response => response.json());
      });
    }
    
    getAll() {
      const batch = [{...},{...},{...},...];
      return Observable.fromPromise(this.authenticationService.getAccessToken())
                       .flatMap(accessToken => {
        const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
        return this.http.post('https://graph.facebook.com', body)
                        .retry(3)
                        .map(response => response.json())
        });
    }
    
    3) 您可以利用
    catch
    操作符来处理错误:

    getAll() {
      const batch = [{...},{...},{...},...];
      return Observable.fromPromise(this.authenticationService.getAccessToken())
                       .catch(() => Observable.of({})) // <-----
                       .flatMap(accessToken => {
        const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
        return this.http.post('https://graph.facebook.com', body)
                        .retry(3)
                        .map(response => response.json())
        });
    }
    
    getAll(){
    常量批=[{…},{…},{…},{…},…];
    返回Observable.fromPromise(this.authenticationService.getAccessToken())
    .catch(()=>可观察到的({}))//{
    const body=`access_token=${accessToken}&batch=${JSON.stringify(batch)}`;
    返回此.http.post('https://graph.facebook.com",正文)
    .重试(3)
    .map(response=>response.json())
    });
    }
    
    在这种情况下,当获取访问令牌发生错误时,将提供一个空对象来构建POST请求


    4) 是的,当然!
    map
    操作符允许您返回您想要的…

    由于某种原因,我无法使
    fromPromise
    工作。因此为了简化,我尝试只订阅
    accessToken
    ,但没有成功。我把它放在我的组件构造函数中:
    Observable.fromPromise(this.authenticationService.getAccessToken()).subscribe(response=>this.response=response,error=>this.response=error)。我还用
    getAccessToken()
    方法更新了我的答案。我看到你使用了一个facebook插件。您的问题是否与更改检测相关。我的意思是数据在你的视图中没有更新。。。是的,我有一个应用程序正在使用。由于我无法使您的代码正常工作,因此我首先尝试使用observables根据您的答案获取
    accessToken
    ,并将其显示在我的视图中,成功后,我将继续post请求。我这里没有使用flatMap()。当我尝试访问该页面时,它不会加载。我用代码的相关部分创建了一个,也许它会帮助您理解我的问题。请尝试此导入:
    import{Observable}from'../../node_modules/rxjs/Rx'该死。你是对的,我从错误的路径导入了
    可观察的
    !!
    accessToken
    正常!现在我将继续处理post请求。