I have a service where I get data by sending a sql query;
getList(model:sqlModel):Observable<apiResultModel<sqlResultModel>>{
return this.http.post<apiResultModel<sqlResultModel>>(this.apiUrl,JSON.stringify(model),httpOptions);
}
My service is running smoothly. but in a component I want to use this incoming data, I have to wait for the data to come.
openInputPopup():void{
const resModel = this.getSqlResult();--this should not go to the bottom line before it is completed
console.log('res',resModel);
const modal = this.inputPopup.create({
nzTitle: 'Component Settings',
nzStyle:{'top':'20px', 'width':'60%'},
nzContent: FormPopupModalComponent,
nzViewContainerRef: this.viewContainerRef,
nzComponentParams: {
sqlResModel:resModel
},
nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
});
modal.afterClose.subscribe(a=>{
var el = document.getElementById('popDeneme');
this.popValue = modal.componentInstance.inputId;
});
}
getSqlResult() :sqlResultModel{
this.sqlModel.dbSourceId=1;
this.sqlModel.limitperPage=1;
this.sqlModel.pageNum=1;
this.sqlModel.query="SELECT [CODE],[DEFINITION_] FROM [TEST].[dbo].[PRODUCTS]"
var res = new sqlResultModel();
this.sqlService.getList(this.sqlModel).subscribe(
i=>{
res = i.Result;
console.log('ok');
}
);
return res; --There should be no return before the getlist() function above is completed. Since getlist() is not complete, it returns the initial value and it doesn't work for me.
}
what do you suggest i do? I know it's a small point but I'm new to coding please help
you can use Rxjs Operators as mentioned before but you can also convert to promise and use await like this :
return this instead return this.sqlService.getList(this.sqlModel).toPromise()
and use it like this :
async openInputPopup():void{
const resModel = await this.getSqlResult();
// your code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.