![](/img/trans.png)
[英]How to return Observable after some Promise get resolved in Ionic 2/Angular 2?
[英]How do I return a value after promise is resolved Angular Typescript?
當我將值插入 SQL 數據庫時,我使用 Angular 和 Typescript 進行驗證,它檢查該值是否已經在數據庫表中。 我從 ASP .NET CORE API 得到響應,如果該值已經在表中,數據庫服務器將返回true 。
回應只是這樣:
true
例如,我在數據庫表中已經有值“1”然后我試圖插入值“1”但它會將它插入到表中,當我再次嘗試插入該值時它會停止並且不會讓我插入它. 如果我刷新頁面,即使數據庫表中有 >10 個“1”值,也會發生相同的過程。 我可以用什么來解決這個問題以及如何解決?
FileCount:boolean;
getFileCount() {
this.http.get<boolean>(this.BaseURL + '/Count/File?Name=' + this.formData.Name)
.subscribe(
result => {
this.FileCount = result;
console.log(this.FileCount);
}
);
}
insertRecord(form: NgForm) {
this.getFileCount();
if (this.FileCount == true) {
this.toastr.warning('Submitted failed', 'There is another file with the name: "' + this.formData.Name + '"');
}
else {
this.postFile().subscribe(
res => {
this.toastr.success('Submitted successfully', 'File Creation Complete');
this.refreshList();
},
(err) => {
console.log(err);
}
);
}
}
postFile() {
return this.http.post(this.BaseURL + '/File', this.formData);
}
出現問題是因為對this.getFileCount()方法的調用是異步運行的,即不等待應答,進程繼續,所以必須等待它的應答。
嘗試這個:
getFileCount() {
return this.http.get<boolean>(this.BaseURL + '/Count/File?Name=' + this.formData.Name);
}
insertRecord(form: NgForm) {
this.getFileCount().subscribe(result => {
this.FileCount = result;
console.log(this.FileCount);
if (this.FileCount == true) {
this.toastr.warning('Submitted failed', 'There is another file with the name: "' + this.formData.Name + '"');
}
else {
this.postFile().subscribe(
res => {
this.toastr.success('Submitted successfully', 'File Creation Complete');
this.refreshList();
},
(err) => {
console.log(err);
}
);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.