Unexpected token. A constructor, method, accessor, or property was expected in Typescrcipt

user3301440 picture user3301440 · Sep 19, 2017 · Viewed 7.4k times · Source

I am trying to Callback after item.upload() using ng2-file-upload in angular 4 but i am getting an error as 'Unexpected token. A constructor, method, accessor, or property was expected' and ' Declaration or statement expected'

Below is the my component and app.module code.

Does any one save my time

Upload Component

                import {  Component } from '@angular/core';
                import * as FileSaver from 'file-saver';
                import * as XLSX from 'xlsx';
                import { FileUploader 
                ,FileItem,ParsedResponseHeaders,FileLikeObject} from 'ng2-file-upload';

                @Component ({  
                   selector: 'my-app',  
                   templateUrl:'./excelUpload.html'

                })  
                export class ExcelUploadComponent  { 

                    private uploader:FileUploader = new FileUploader({url:'http://localhost:5000/upload'});

                    this.uploader.onSuccessItem = (item:FileItem, response:string, status:number, headers:ParsedResponseHeaders) => 
                    { console.log("onSuccessItem " + status, response, item); 
                        if(response) 
                        { //parse your response.
                        } 
                    }
                }

app.module.ts

            import { BrowserModule } from '@angular/platform-browser';
        import { NgModule } from '@angular/core';
        import { FormsModule,ReactiveFormsModule} from '@angular/forms'; 
        import { HttpModule } from '@angular/http';
        import { AppComponent } from './app.component';
        import { RouterModule, Routes } from '@angular/router';
        import {ExcelUploadComponent} from './ExcelUpload/ExcelUpload.Component';
        import {  Component } from '@angular/core';
        import { FileSelectDirective, FileDropDirective } from 'ng2-file-upload';
        import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';



        @NgModule({
          declarations:[ AppComponent,
             ExcelUploadComponent
            ,FileDropDirective, FileSelectDirective ],
          imports:    [ BrowserModule , FormsModule,HttpModule],
          providers: [],
          bootstrap: [LayoutComponent]

        })
        export class AppModule { }

Answer

Saravana picture Saravana · Sep 19, 2017

Only class members should be directly inside the class. Any code that needs to be executed as part of class initialization should be in the constructor:

// ...
export class ExcelUploadComponent {

    private uploader: FileUploader = new FileUploader({ url: 'http://localhost:5000/upload' });
    constuctor() {
        this.uploader.onSuccessItem = (item: FileItem, response: string, status: number, headers: ParsedResponseHeaders) => {
            console.log("onSuccessItem " + status, response, item);
            if (response) { //parse your response.
            }
        }
    }

}