這是錯誤:
ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
這是代碼
publicresourceuploadtos3(event): Observable<any>{
console.log('it got inside the public resource upload to s3 ');
const mediatobeuploaded = event.target.files[0];
return this.http.get(environment.public_generate_presigned_url_resource).pipe(
switchMap((req : any)=>{
const resourceurlprovided = req.uriroot req.fields.key;
console.log('it got the public presigned url');
let fd = new FormData();
fd.append('acl', req.fields.acl);
fd.append('key', req.fields.key);
fd.append('content-type', req.fields['content-type']);
fd.append('policy', req.fields.policy);
fd.append('x-amz-algorithm', req.fields['x-amz-algorithm']);
fd.append('x-amz-credential', req.fields['x-amz-credential']);
fd.append('x-amz-date', req.fields['x-amz-date']);
fd.append('x-amz-signature', req.fields['x-amz-signature']);
fd.append('file', mediatobeuploaded);
return this.http.post(req.url, fd, {
reportProgress: true,
observe: 'events'
}).pipe(
// answer two https://stackoverflow.com/questions/51176537/angular-6-and-node-js-aws-s3-file-upload-progress-bar
switchMap((events: any)=>{
console.log('it got into the swtich map');
switch(events.type){
case HttpEventType.UploadProgress:
const result = {
progressreport: true,
progress: Math.round(events.loaded / events.total * 100)
};
console.log('it went into upload progress');
return of(result);
case HttpEventType.Response:
const result2 = {
progressreport: false,
resourceurl: resourceurlprovided,
resourcekey: req.fields.key
};
console.log('it went into the response');
return of(result2);
}
}));
}));
}
注意這些console.log()陳述。最后一個開火的是console.log('it got into the switch map')
這意味著我的 switch 映射呼叫了這個錯誤,因為它期望回傳 observables。
但看看這段代碼。
switch(events.type){
case HttpEventType.UploadProgress:
const result = {
progressreport: true,
progress: Math.round(events.loaded / events.total * 100)
};
console.log('it went into upload progress');
return of(result);
case HttpEventType.Response:
const result2 = {
progressreport: false,
resourceurl: resourceurlprovided,
resourcekey: req.fields.key
};
console.log('it went into the response');
return of(result2);
}
}));
哪些return of陳述為什么不滿足?我究竟做錯了什么?我的理解是,回傳是一個可觀察的。
我的部分思考程序是,如果兩個 switch case 陳述句不起作用,我需要有一個默認的陳述句。但我試過了,我的 IDE 一直對我大喊大叫。所以要么我在做一些完全愚蠢的事情,要么我錯過了一些重要的事情。
uj5u.com熱心網友回復:
- 如果您
of(result)在 a 內回傳switchMap(),請map()改用。您可以簡單地回傳結果而不將其包裝在of(). - 您的 switch 塊處理兩種情況,但如果兩種情況都失敗了怎么辦?那是您不回傳任何東西的時候。如果兩種情況都失敗,請嘗試在最后添加默認情況以回傳某些內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416718.html
標籤:
上一篇:按類別分組物件陣列
