1

I am moving an angular 2 project to angular 4 and I am also updating the angular-cli to the current 1.0 version. there is no AOT in the app.

I have a component in my app that is throwing errors in the build process but not in the IDE (Visual Studio 2017 professional).

The following error is thrown during build:

ERROR in .../src/app/shift-list/shift-list.component.ts (163,88): Property 'userRank' does not exist on type 'void'.

ERROR in .../src/app/shift-list/shift-list.component.ts (176,90): Property 'userPlatoon' does not exist on type 'void'.

THe following is my component (stripped down for brevity):

@Component({
  selector: 'ksm-shift-list',
  templateUrl: './shift-list.component.html',
  styleUrls: ['./shift-list.component.css'],
  providers: [
      DnnService
  ]
})
export class ShiftListComponent implements OnInit {

    ...
    userRank: string;
    userPlatoon: string;
    ...

    constructor(
        private _dnn: DnnService) { }

    // On Init  ********************************************************
    ngOnInit(): void {
        this.getDepartmentAndOptions();
    }

    // #################################################################
    //                        LIST OPTIONS
    // #################################################################

    getDepartmentAndOptions(): void {
        this._dnn.getOptionsAndProfile()
            .subscribe(
            data => {
                this.appOptions = data;

                this.ptnList = this.appOptions.Platoons;
                this.rankList = this.appOptions.Ranks;

                if (this.appOptions.User != null) {
                    this.userRank = this.appOptions.User.RankID;
                    this.userPlatoon = this.appOptions.User.PlatoonID;
                }
            }
            ),
            (err: any) => { // on error
                console.log(err);
            },
            () => { // on completion 

                this.sortRanks();
                this.sortPlatoons();
            }
    }


    // SORT RANK FUNCTION  **************** 
    sortRanks(): void {

        // sort rank list
        this.rankList.sort((b1: Rank, b2: Rank): number => {
            if (b1.Priority < b2.Priority) { return -1; };
            if (b1.Priority > b2.Priority) { return 1; };
            return 0;
        });

        // assign existing rank
        this.selectedRank = this.rankList.filter(function (r) { return r.Code === this.userRank })[0].RankID
    }

    // SORT PLATOONS FUNCTION   ***********
    sortPlatoons(): void {
        // sort results
        this.ptnList.sort((p1: Platoon, p2: Platoon): number => {
            if (p1.PlatoonID < p2.PlatoonID) { return -1; };
            if (p1.PlatoonID > p2.PlatoonID) { return 1; };
            return 0;
        });

        // assign existing platoon
        this.selectedPlatoon = this.ptnList.filter(function (p) { return p.Code === this.userPlatoon })[0].PlatoonID;
    }

}

The error I mention is thrown on the following two lines:

this.selectedRank = this.rankList.filter(function (r) { return r.Code === this.userRank })[0].RankID

and

this.selectedPlatoon = this.ptnList.filter(function (p) { return p.Code === this.userPlatoon })[0].PlatoonID;

I did not change any code and no other components with identical code throw this error.

Can someone help me figure out why this error is generated during the cli build process

Thanks in advance

J King
  • 4,108
  • 10
  • 53
  • 103

1 Answers1

2
this.ptnList.filter(function (p) { ..

should be

this.ptnList.filter((p)=> { ..

and the same for

this.rankList.filter(function (r) { 

should be

this.rankList.filter((r)=> {

If you use function, this will refer to that callbacks instance

Suggested reading: How to access the correct `this` inside a callback?

eko
  • 39,722
  • 10
  • 72
  • 98