首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调试返回到角可观测订阅服务器的对象类型?

如何调试返回到角可观测订阅服务器的对象类型?
EN

Stack Overflow用户
提问于 2018-11-26 09:36:57
回答 1查看 1.9K关注 0票数 1

我是Range-7RxJS-6Visual代码的新手,我很难调试正在返回给订阅服务器的可观察到的代码,结果是订阅服务器提出了一个运行时"TypeError“。从研究中可以看出,我并不是一个人有这样棘手的问题。您能建议我如何确定订阅者在“观察”什么,或者您能发现下面代码中的错误吗?

详细

我正在编写一个非常简单的概念证明,使用Visual和角-7cli从使用httpclient的服务器检索当前系统日期/时间,并显示它。

请参阅下面的instrument.service.ts::getSystemTimeDate()方法。HTTP层很好,因为获得了JSON响应.

代码语言:javascript
复制
{
  "SystemDateTime": "2018-11-26T08:54:06.894Z"
}

map操作符中,这个响应首先转换为SystemDateTimeResponse类型的对象,然后转换为Date,该方法应该将Observable<Date>返回给任何订阅者。我遇到的问题是组件对Observable<Date>的订阅。在运行时,方法onTimeDateBtnClick()中的此订阅服务器引发一个错误:

代码语言:javascript
复制
ERROR
TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
message: "You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."
stack: "TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at subscribeTo (http://localhost:4200/vendor.js:75870:15)
    at subscribeToResult (http://localhost:4200/vendor.js:76027:76)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (http://localhost:4200/vendor.js:70784:90)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://localhost:4200/vendor.js:70778:14)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (http://localhost:4200/vendor.js:70761:18)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://localhost:4200/vendor.js:65218:18)
    at TapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/tap.js.TapSubscriber._next (http://localhost:4200/vendor.js:73228:26)
    at TapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://localhost:4200/vendor.js:65218:18)
    at TakeSubscriber.push../node_modules/rxjs/_esm5/internal/operators/take.js.TakeSubscriber._next (http://localhost:4200/vendor.js:72950:30)
    at TakeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://localhost:4200/vendor.js:65218:18)"
__proto__: Error {constructor: , name: "TypeError", message: "", …}
constructor: function TypeError() { … }
message: ""
name: "TypeError"
toString: function toString() { … }
__proto__: Object {constructor: , name: "Error", message: "", …}

我相信我没有正确地返回一个可观察到的,可能是搞砸了我对地图操作员的使用。我遗漏了什么?

代码

此片段的软件引用包括:

timedate.component.html:包含以下简单模板:

代码语言:javascript
复制
<p>
  Last time I checked, it was : {{today | date:'medium'}}
</p>
<button mat-button (click)="onTimedateBtnClick()">Update</button>

timedate.component.ts:包含today的display属性定义和事件处理程序onTimedateBtnClick(),后者使用数据服务管理HTTP /response,从服务器检索当前日期/时间。

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { InstrumentService } from '../instrument.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-timedate',
  templateUrl: './timedate.component.html',
  styleUrls: ['./timedate.component.css']
})
export class TimedateComponent implements OnInit {

  /** Display property */
  today: Date;

  /**
   * Constructor
   * @param - data service
   */
  constructor(private dataService: InstrumentService) {
  }

  ngOnInit() {
    this.today = new Date();  /// initialise with client's date/time
  }

  /**
   *  User event handler requesting system time/date from the server
   */
  onTimedateBtnClick() {
    const http$: Observable<Date> = this.dataService.getSystemTimeDate();

    http$.subscribe(
      res => this.today = res,
    );
  }
}

instrument.service.ts:包含返回Observable<Date>getSystemTimeDate()方法。同样,我简化了代码(尽管它仍然失败),并夸大了映射,以便更好地查看我正在做的事情。

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';

// App imports
import { SystemDateTimeResponse, SystemDateTimeUrl } from './instrument.service.httpdtos';


@Injectable({
  providedIn: 'root'
 })
export class InstrumentService {

  constructor(private http: HttpClient) { }

  /**
   * Return the server date and time
   */
  public getSystemTimeDate(): Observable<Date> {
    // Convert the server response object into an observable date
    const responseObject: Observable<Date> =
    this.http.get<SystemDateTimeResponse>(SystemDateTimeUrl.Url).
      pipe(
        map(jsonResponse => {
          const newDto = new SystemDateTimeResponse(jsonResponse.SystemDateTime);
          const d = new Date(newDto.SystemDateTime);
          return d;
        }),
      );

    return responseObject;
  }
}

instrument.service.httpdtos.ts:包含数据传输对象定义。

代码语言:javascript
复制
/** URL - Instrument system date/time */
export class SystemDateTimeUrl {
  public static readonly HttpVerb = 'GET';
  public static readonly Url = 'api/instrument/systemdatetime';
  public static readonly Summary = 'Query the instrument current date/time';
}

/** Response DTO */
export class SystemDateTimeResponse {
  constructor(
    public SystemDateTime: string     // In UTC format
  ) { }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-26 10:38:20

你有两个选择。如果您正在使用Chrome开发此应用程序,您可以使用开发工具,在webpack中找到您的源代码,并在服务中添加多个断点以进行调试。最棘手的部分是找到消息来源。从那里开始,应该是海峡前进。

第二个选项,如果使用Intellij / WebStorm,可以在编辑器中调试应用程序。为此,必须在Chrome / Firefox中安装JetBrains IDE支持扩展,然后必须配置编辑器以添加新的配置:编辑器配置-> Javascript。您必须指定正确的端口(如果应用程序运行在不同的端口上)

启动应用程序和调试配置之后,在代码中添加一个新的断点(对订阅/映射函数进行回调),并且应该能够检查您拥有哪些变量。

如果你还有其他问题,请不要犹豫。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53478229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档