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
Angular 角度-不命中http响应回调_Angular_Http_Callback - Fatal编程技术网

Angular 角度-不命中http响应回调

Angular 角度-不命中http响应回调,angular,http,callback,Angular,Http,Callback,嗨,我正试图对http请求的结果做些什么,但我的回调从未被执行过,我不知道为什么 从这件事开始,我就遵循这个例子 这是我的代码: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import * as moment from "moment"; import 'rxjs/add/operator/do'; import 'rxjs/add/operator

嗨,我正试图对http请求的结果做些什么,但我的回调从未被执行过,我不知道为什么

从这件事开始,我就遵循这个例子

这是我的代码:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as moment from "moment";
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/shareReplay';

@Injectable()
export class AuthdataService {
constructor(private http: HttpClient) {}

login(email: string, password: string) {

  return this.http
    .post("http://localhost:3000/user/login", {
      email,
      password
    })
    .do(res => this.setSession)
    .shareReplay();
  }

  private setSession(authResult) {
  console.log("Why am I not being hit???")
  const expiresAt = moment().add(authResult.expiresIn,'second');

  localStorage.setItem('id_token', authResult.idToken);
  localStorage.setItem("expires_at", JSON.stringify(expiresAt.valueOf()) );
  }  
}

更新的解决方案:

login(email: string, password: string) {

return this.http
  .post("http://localhost:3000/user/login", {
    email,
    password
  })
  .do(res => this.setSession(res))
  .shareReplay();   
  }

您希望您正在创建的
可观察的
序列是“热的”,但它是“冷的”。我无法用更好的术语描述这种差异

换句话说,
.post
在订阅之前实际上什么都不做

调用
shareReplay
不会订阅
可观察的
,但只能确保一次最多创建一个对基础
可观察的
的订阅


因此,缺少的链接可能是对您创建的
可观察的
上的
.subscribe
的调用。

您希望您创建的
可观察的
序列是“热的”,而不是“冷的”。我无法用更好的术语描述这种差异

换句话说,
.post
在订阅之前实际上什么都不做

调用
shareReplay
不会订阅
可观察的
,但只能确保一次最多创建一个对基础
可观察的
的订阅


因此,缺少的链接可能是对您创建的
可观察的
上的
.subscribe
的调用。

您好,谢谢您的回答-我很惊讶Angular University示例不起作用。因此,如果我在shareReplay()之后添加“.subscribe(this.setSession);”它就起作用了但是我需要do语句来调用shareReplay,但是.do()的参数中应该有什么呢?因为这是一个服务,如果我在这里添加一个subscribe,我就不能在我的组件中subscribe。。我最终找到了一个解决方案,这基本上只是一个愚蠢的错误。@J.Kirk。我看到了你更新的解决方案。对不起,我没注意到!您好,谢谢您的回答-我很惊讶Angular大学的示例不起作用。因此,如果我在shareReplay()之后添加“.subscribe”(this.setSession);”它会起作用,但我需要执行do语句来调用shareReplay,但是.do()的参数中应该包含什么呢?因为这是一项服务,如果我在此处添加subscribe,我就无法在组件中分包。。我最终找到了一个解决方案,这基本上只是一个愚蠢的错误。@J.Kirk。我看到了你更新的解决方案。对不起,我没注意到!