【发布时间】:2019-07-23 00:48:49
【问题描述】:
这很令人困惑。我正在使用 angular(ionic) 并调用两个链承诺,然后希望返回如下可观察到的。我尝试在两个 Promise 前添加 return,但当然会引发编译错误。
Type 'Promise<Observable<{ formInstanceId: any; questionId: any; repeatId: any; filename: any; isEquipment: any; }>>' is not assignable to type 'Observable<any>'. Property '_isScalar' is missing in type 'Promise<Observable<{ formInstanceId: any; questionId: any; repeatId: any; filename: any; isEquipment: any; }>>'
我曾想过使用 from(promise1) 和 from(promise2) 但后来变得更加混乱..
我想将其更改为完全可观察的主要原因是因为我想在下面代码末尾的 post() 中使用 timeout()
public uploadFile(formInstanceId, questionId, repeatId, filePath, isEquipment): Observable<any> {
this._file.resolveLocalFilesystemUrl(filePath).then(result => {
console.log("fileSelected", result);
let fileName = result.name;
let nativeURL = result.nativeURL;
let path = nativeURL.substring(0, nativeURL.lastIndexOf("/"));
this._file.readAsArrayBuffer(path, fileName).then(buffer => {
let imgBlob = new Blob([buffer], {type: "image/jpeg"});
let params = {fileName: 'attachment'};
let query = this.encodeQueryData(params);
let uploadFileURL = this._networkService.serverUrl + '/api/upload?' + query;
return this._networkService.postFile(uploadFileURL, fileName, imgBlob).pipe(map(response => {
console.log(response);
return {
formInstanceId: formInstanceId,
questionId: questionId,
repeatId: repeatId,
filename: response[0].filepath,
isEquipment: isEquipment
};
},
error => {
return throwError(error);
}
));
});
})
}
【问题讨论】:
-
你到底遇到了什么错误?
-
@SmokeyDawson 更新问题
-
您是否有理由要返回 Observable 而不是 Promise?
-
@SmokeyDawson 在 network.postFile 中使用超时。而不是使用 .subscribe(),pipe(map) 甚至没有到达..
标签: angular promise observable