What im doing wrong to get this error?
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-uploader',
templateUrl: './uploader.page.html',
styleUrls: ['./uploader.page.scss'],
})
export class UploaderPage implements OnInit {
imageURL: string
constructor(public http: HttpClient) { }
ngOnInit() {
}
fileChanged(event) {
const files = event.target.files
const data = new FormData()
data.append('file', files[0])
data.append('UPLOADCARE_STORE', '1')
data.append('UPLOADCARE_PUB_KEY', 'my key')
this.http.post('https://upload.uploadcare.com/base/', data).subscribe(event => {
console.log(event)
this.imageURL = event.json().file
})
}
}
But when i upload a photo i get this error and also i get error that json does not exist on type 'Object'
ERROR TypeError: Cannot read property 'target' of undefined at UploaderPage.fileChanged (uploader.page.ts:16) at Object.eval [as handleEvent] (UploaderPage.ngfactory.js:26) at Object.handleEvent (core.js:43993) at Object.handleEvent (core.js:44774) at dispatchEvent (core.js:29804) at core.js:42925 at HTMLInputElement. (platform-browser.js:2668) at ZoneDelegate.invokeTask (zone-evergreen.js:391) at Object.onInvokeTask (core.js:39680) at ZoneDelegate.invokeTask (zone-evergreen.js:390)
EDITED: This is the HTML "caller"
<ion-header>
<ion-toolbar>
<ion-title>Upload Image</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<div class="camera"> </div>
<input type="file" (change)="fileChanged()"/>
<img *ngIf="imageURL" src="https://ucarecdn.com/{{ imageURL}}/"/>
</ion-content>
UPDATE
The first part of the problem i got fixed by alexortizl (the accepted answer below).
The json part i fixed it from this source where i just need to remove json.parse because the url was already in json format.
eventis not being passed to the function correctly? can you share the caller?