Seeking assistance with resolving an issue involving an undefined error when attempting to make an http request within a Promise function. The error occurs due to this.http.post
being undefined, indicating that there is an issue with accessing this
properly.
If anyone has insights or suggestions on how to resolve this issue, your help would be greatly appreciated!
doUpload(files: Array<File>): Promise<Array<UploadResult>> {
console.log(files);
return new Promise((resolve, reject) => {
setTimeout(() => {
let result: Array<UploadResult> = [];
for (let file of files) {
this.http
.post(this.APIURL + "/image/upload/markdown", file)
.subscribe((data) => {
console.log(data);
});
result.push({
name: file.name,
url: `https://avatars3.githubusercontent.com/${file.name}`,
isImg: file.type.indexOf("image") !== -1,
});
}
resolve(result);
}, 3000);
});
}
The encountered error message:
> TypeError: undefined is not an object (evaluating 'this.http.post')
> (anonymous function) — sensor-edit.component.ts:308
> onInvokeTask — core.js:39680
> runTask — zone-evergreen.js:168
> invokeTask — zone-evergreen.js:465
> timer — zone-evergreen.js:2650
This error originates from the constructor, where this.http
is defined:
constructor(
private fb: FormBuilder,
private route: ActivatedRoute,
private api: ApiService,
private _routerService: Router,
private errorService: ErrorModalService,
private sanitizer: DomSanitizer,
private http: HttpClient
) {}
The doUpload
function is invoked in the HTML as follows:
<div class="container well come-space">
<md-editor formControlName="markdown" name="Content" [height]="'400px'" [upload]="doUpload">
</md-editor>
</div>