我已经编写了两种从asp.net api获取数据的方法。 在chrome开发人员工具的“网络”标签中,响应(来自服务器的到达数据)是正确的, 但是方法1遇到此错误:
Error: scrs.find is not a function
at MapSubscriber.eval [as project]
and scrs[in method 2] is undefined.
请问有人可以帮助我吗?
// -------------方法1 ---------------------------
// ------------在MySerivce中-------------------------
this.scoreStore = {scores:[]};
this.scoreSubjext = <BehaviorSubject<IScore[]>> new BehaviorSubject([]);
this.scores = this.scoreSubjext.asObservable();
GetScore(st)
{
var header = new HttpHeaders({ "Content-Type": "application/json" });
return this.httpClnt.post(this.BASE_URL+this.URL_GET_SCORES,st,{headers: header})
.subscribe((resp:any)=>{
this.scoreStore.scores = resp;
this.scoreSubjext.next(Object.assign({},this.scoreStore).scores);
},catchError((error: any) => ErrorObservable.create(error))
);
}
// ------------------调用该方法并找到所需的分数
this.myService.GetScore(this.st);
this.score = this.mySerrvice.scores.pipe(map(scrs =>{
return scrs.find(scr => scr.id === id);
}));
<div *ngIf="(score | async | json) as userScore">
<li>{{userScore.grade}}</li>
<li>{{userScore. ...}}</li>
</div>
//???????????????
Error: scrs.find is not a function
at MapSubscriber.eval [as project]
浏览器开发人员工具的“网络”标签中有有效的响应。
// -----------------------方法2 ----------------
// -----------在MySerivce中-----------
GetScore2(st)
{
var header = new HttpHeaders({ "Content-Type": "application/json" });
return this.httpClnt.post(this.BASE_URL+this.URL_GET_SCORES,st,{headers:
header}).toPromise();
}
// ------------------------调用方法------------------ -----
let scrs = await this.myService.GetScore2(this.st);
console.log(scrs);
// ?????????????????????
当浏览器开发人员工具的“网络”标签中存在有效的响应时,scrs尚未定义。
非常感谢您。