webdad3
webdad3

Reputation: 9080

properties are undefined inside of promises

This is my typescript function where I'm trying to use a promise:

public onEditSubmit() {
    const model = this._sharedService.createUpdateModel(
        null,
        this.editForm
    ) as LOG;

    model.fileId = this.fileId;
    model.startEffectiveDate = Shared.toISODate(model.startEffectiveDate);
    model.endEffectiveDate = Shared.toISODate(model.endEffectiveDate);



    let deferredExecutionCheck = new Promise((resolve, reject) => {
        this._updateService
            .getAllById(this.selectedItem.LogId)
            .subscribe(
                r => {
                    this.records = r;

                    this.records.forEach(element => {
                        if (
                            element.StatusId === 1 ||
                            element.StatusId === 2 ||
                            element.StatusId === 4 ||
                            element.StatusId === 5
                        ) {
                            this._notificationService.showErrorMessage(
                                `MESSAGE GOES HERE`,
                                "IN PROGRESS"
                            );

                            reject("In Progress");
                        }
                    });
                    resolve("Not In Progress");
                },
                e => {
                    throw e;
                }
            );
        console.log("finished");
    });

    let originalEditSubmit = function(result: any) {
        if (this.editMode === "Add") {
            this.add(model);
        } else {
            if (
                (model.wfStatusId === Status.Review ||
                    model.wfStatusId === Status.LoadFailed ||
                    model.wfStatusId === Status.Completed) &&
                model.eventStatusId === eStatus.Cancelled
            ) {
                this._confirmDlg.closable = false;
                this._confSvc.confirm({
                    accept: () => {
                        model.cancelRcdb = true;
                        this.update(model);
                    },
                    message: "Cancel RCdB Dataset?",
                    reject: () => {
                        model.cancelRcdb = false;
                        this.update(model);
                    }
                });
            } else {
                this.update(model);
            }
        }
    };

    deferredExecutionCheck.then(
        result => originalEditSubmit(result),
        error => console.log("error", error)
    );
}

Error: Uncaught (in promise): TypeError: Cannot read property 'editMode' of undefined TypeError: Cannot read property 'editMode' of undefined at originalEditSubmit

I moved the this.fileId property outside of the originalEditSumbmit method and it now is being read. But now it seems like this.editMode is now having the same issue.

Can I not have these properties inside of my promises like this?

Upvotes: 0

Views: 1013

Answers (1)

Reza
Reza

Reputation: 19843

change

let originalEditSubmit = function(result: any) {

to

let originalEditSubmit = (result: any) => {

Upvotes: 1

Related Questions