Metodo upload do photo.service
upload(description: string, allowComments: boolean, file: File) {
const formData = new FormData();
formData.append('description', description);
formData.append('allowComments', allowComments ? 'true' : 'false');
formData.append('file', file);
return this.http.post(API + '/photos/upload', formData);
}
Class PhotoComponent
export class PhotoComponent {
private _url = '';
@Input() description = '';
@Input() set url(url: string) {
if (!url.startsWith('data')) {
this._url = cloud + url;
} else {
this._url = url;
}
}
get url() {
return this._url;
}
}
Metodo upload do photo-form.component
upload() {
const description = this.photoForm.get('description').value;
const allowComments = this.photoForm.get('allowComments').value;
this.photoService.upload(description, allowComments, this.file)
.subscribe(
() => this.router.navigate(['']),
err => console.log(err)
);
}
Erro lançado no console do browser
POST http://localhost:3000/photos/upload 500 (Internal Server Error)
photo-form.component.ts:36 HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "Internal Server Error", url: "http://localhost:3000/photos/upload", ok: false, …}error: {message: "Internal server error"}headers: HttpHeaderslazyInit: () => {…}lazyUpdate: nullnormalizedNames: Map(0) {}__proto__: Objectmessage: "Http failure response for http://localhost:3000/photos/upload: 500 Internal Server Error"name: "HttpErrorResponse"ok: falsestatus: 500statusText: "Internal Server Error"url: "http://localhost:3000/photos/upload"__proto__: HttpResponseBaseconstructor: class HttpErrorResponse__proto__: Object
Erro lançado no terminal do servidos node
Valid token received: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6ImZsYXZpbyIsImVtYWlsIjoiZmxhdmlvQGFsdXJhcGljLmNvbS5iciIsImlhdCI6MTU2MzIxNzM4OSwiZXhwIjoxNTYzMzAzNzg5fQ.V8leJt-GBrd1jMN9beXZZWEaulHJdw9mNl0JBXBkagQ
Error: Unexpected field
at makeError (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\multer\lib\make-error.js:12:13)
at wrappedFileFilter (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:198:13)
at Busboy.emit (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:198:13)
at HeaderParser.<anonymous> (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:198:13)
at HeaderParser._finish (C:\Users\mamaral\Documents\Cursos Alura\alurapic\api\node_modules\dicer\lib\HeaderParser.js:68:8)