【问题标题】:Fetch http data before send http request in Angular 2 with RxJS在使用 RxJS 在 Angular 2 中发送 http 请求之前获取 http 数据
【发布时间】:2016-03-27 13:22:43
【问题描述】:

我有注册表,我需要发送带有 csrf 令牌的表格,我应该在发送填写的表格之前得到它。没有工作代码,但它知道我想要什么。

var model = {
            username : this.form.value.username,
            email : this.form.value.email,
            password_second : this.form.value.password_second,
            password : this.form.value.password,
            csrf : ''
        };
        this._csrfService.getToken().subscribe(
            csrf => model.csrf,
            error => console.log(error)
        );
        this._signUpService.sendForm(model)
            .subscribe(
                hero  => console.log(hero),
                error =>  console.log(error));

SignUp 和 Csrf 服务很明显:

getToken()
    {
        console.log(this.http.get(this._sighUpUrl));
        return this.http.get(this._sighUpUrl)
            .map(res =>  res.json().data.csrf)
            .catch(this.handleError)
    }

sendForm(name:Object)
    {
        let body = JSON.stringify(name);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        console.log(this.http.post(this._sighUpUrl, body, options));
        return this.http.post(this._sighUpUrl, body, options)
            .map(res =>  res.json().data)
            .catch(this.handleError)
    }

当我没有得到 csrf 令牌时如何等待?

【问题讨论】:

  • 只是从第一个回调中发送第二个请求
  • 我无法从回调中访问“this”
  • 你为什么不能访问这个?
  • 我有错误:“例外:类型错误:无法读取未定义的属性‘值’”。 console.log(this) 也显示它是 Observable 类,而不是我的组件
  • lambda 中的 this 始终指代外部作用域的 this。如果 this 是一个 observable,这意味着你没有使用 lambda,你使用了一个匿名函数。

标签: angular rxjs angular2-services


【解决方案1】:

你可以从第一个回调中发送第二个请求:

var model = {
    username : this.form.value.username,
    email : this.form.value.email,
    password_second : this.form.value.password_second,
    password : this.form.value.password,
    csrf : ''
};
this._csrfService.getToken().subscribe(
    csrf => {
      model.csrf = csrf;
      this._signUpService
        .sendForm(model)
        .subscribe(
          hero  => console.log(hero),
          error =>  console.log(error)
        );
    },
    error => console.log(error)
);

或者更好地使用可观察对象的组合运算符:

var model = {
    username : this.form.value.username,
    email : this.form.value.email,
    password_second : this.form.value.password_second,
    password : this.form.value.password
};
this._csrfService
  .getToken()
  .map(csrf => Object.assign({csrf:csrf}, model))
  .flatMap(model => this._signUpService.sendForm(model))
  .subscribe(
    hero  => console.log(hero),
    error =>  console.log(error)
  );

【讨论】:

  • 最好使用flatMapLatest(Rx5 中的switchMap)而不是flatMap
【解决方案2】:

您可以使用 observables 的 flatMap 运算符,如下所述:

getToken().flatMap(token => {
  return sendForm(...);
}).subscribe(...);

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2017-11-07
    • 2015-11-21
    • 2015-12-30
    • 1970-01-01
    • 2016-11-17
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多