如何在tap中查看json的值

  • 本文关键字:json 的值 tap angular rxjs
  • 更新时间 :
  • 英文 :


当我做类似的事情时

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

我看到的结果如下:

returned contact object/object

我想检查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;})
);
}

最新更新