TypeError: Can not read property 'name' of undefined

1

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));
                           });
      }
    
asked by Efrain Peralta 27.06.2018 в 23:45
source

3 answers

0
  

Error: Uncaught (in promise): TypeError: Can not read property 'name'   of undefined TypeError: Can not read property 'name' of undefined

There the error clearly tells you, "can not read property 'name' of undefined" and in the only place where you use the property name is in this line:

formData.append(name, files[i], files[i].name);

which means that files[i] equals undefined

Look for a way to debug your code and check it out

    
answered by 28.06.2018 в 00:05
0

Try a foreach like this:

const formData: FormData = new FormData();
files.forEach(file => {
  formData.append('fileItem[]', file, file.name);
});

Instead of fileItem you can go to the name you want or the variable.

    
answered by 28.06.2018 в 00:07
0

Try modifying the code inside the promise like this:

var fileInput = document.getElementById("myfileinput");
var files = fileInput.files; // Esta línea es la que no tienes
var file;

// loop through files
for (var i = 0; i < files.length; i++) {
  // get item
  file = files.item(i);
  // or
  file = files[i];

  console.log('File to upload: ', file.name);

}

I mean, I think what you need is to add to the files:

var files = fileInput.files;

    
answered by 28.06.2018 в 00:42