如何在tap中查看json的值?

当我做这样的事情

tap(_ => {console.log('returned contact ' + _)}),

我看到这样的结果。

returned contact object/object

我想检查一下json的值。

但当我这样做的时候,我似乎无法让持有联系人JSON的主体查看该行的值。

updateContact(contact: Contact): Observable<Contact>{
    console.log('starting ContactService.updateContact ID' + contact.id + ' name ' + contact.name);
    return this.httpClient.put(this.url, contact, { observe: 'response' })
      .pipe(
        tap(x => {let contact: Contact;
                  contact =  x.body;
                  console.log('returned contact ' + contact.id + ' '+ contact.name + ' ' + contact.email + ' ' + contact.address)}),
        map(x => {return x.body as Contact;})
    );
  }

我似乎无法让持有联系人JSON的主体查看行内的值。

contact =  x.body;

返回一个错误的

ERROR in src/app/service/contact.service.ts(40,23): error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
  Type 'Object' is missing the following properties from type 'Contact': "id", "name", "email", "address"

我的问题是,在tap中,是否有办法查看JSON中的值?

解决方案:

这是我要找的东西。

  updateContact(contact: Contact): Observable<Contact>{ // must be called with a subscribe to work, an An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method.
    console.log('starting ContactService.updateContact ID' + contact.id + ' name ' + contact.name);
    return this.httpClient.put(this.url, contact, { observe: 'response' })
      .pipe(
        tap(x => {          
          //let contact: Contact;
                  let contact: Contact = x.body as Contact;
                  console.log('x.body ' + x.body)
                  console.log('returned contact ' + contact.id + ' '+ contact.name + ' ' + contact.email + ' ' + contact.address)
                }),
        map(x => {return x.body as Contact;})
    );
  }

给TA打赏
共{{data.count}}人
人已打赏
解决方案

向Fluentd发送TCP数据包

2022-4-22 14:08:50

解决方案

为什么Stack要在JDK中扩展Vector?

2022-4-22 14:08:52

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索