I have the following code in a service and it sends me the following error by console:
Error: Uncaught (in promise): TypeError: Can not read property 'name' of undefined TypeError: Can not read property 'name' of undefined
Apparently not recognizing any of the parameters
If anyone has any ideas! Thanks in advance!
import { Injectable } from '@angular/core';
import { GLOBAL } from './global';
@Injectable()
export class UploadService{
public url: string;
constructor(){
this.url = GLOBAL.url;
}
//al parecer no reconoce estos parametros
makeFileRequest(url: string, params: Array<string>, files: Array<File>, token: string, name: string){
return new Promise(function(resolve, reject){
var formData: any = new FormData();
var xhr = new XMLHttpRequest();
// El for dice que no reconoce el namespace Files
for(var i = 0; < files.length; i++){
formData.append(name, files[i], files[i].name);
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
resolve(JSON.parse(xhr.response));
}else{
reject(xhr.response);
}
}
}
xhr.open('POST', url, true);
xhr.setRequestHeader('Authotization', token);
xhr.send(formData);
});
}
}
From here I call the service method
this._uploadService.makeFileRequest(this.url+'upload-image-user/'+this.user._id, [], this.filesToUpload, this.token, 'image')
.then((result: any) => {
console.log(result);
this.user.image = result.image;
localStorage.setItem('identity', JSON.stringify(this.user));
});
}