Новое обещание внутри потом ничего не вернуть - PullRequest
0 голосов
/ 01 марта 2020

Я использую собственный календарь ioni c 3 для добавления новых событий в календарь устройства. Я хотел бы иметь функцию, когда я могу определить, есть ли событие в календаре или нет.

Сначала я проверяю, есть ли у календаря права на чтение / запись, если так, то я бы хотел проверить, существует ли событие, если нет Создай. Проблема, вероятно, заключается в функции checkIfEventExists.

checkCalendarUsePermission() {
        this.calendar.hasReadWritePermission()
            .then((isPermitted) => {
                if (isPermitted) {
                    return true;
                }

                return this.calendar.requestReadWritePermission()
            })
            .then(() => {
                return this.checkIfEventExists();
            })
            .then((result) => {
                console.log('Result non exist', result);
                if(result){
                    console.log(result);
                    this.createNewEvent()
                }

            })
            .catch((err) => {
                console.log('Error occured while getting permission', err)
                this.appFunctionCtrl.presentToast('Error with accessing the write permission', 1500, 'bottom')
            })
    }

checkIfEventExists () {
        console.log('data provided', this.data.title);
        return this.calendar.findEvent(this.data.title)
                .then((result: any) => {
                    console.log('found Event', result)
                    if(result){
                        return result;
                    }
                    return false;
                })
                .catch((err) => console.error('error with findEvent', err))
    }

    createNewEvent() {
        const startDate = moment(this.data.dateFrom, 'DD.MM.YYYY').toDate();
        const cleanDescription = this.removeHtmlTags(this.data.description);
        const options = {
            id: this.data.title
        }
        let endDate = moment(this.data.dateFrom, 'DD.MM.YYYY').toDate();
        if (this.data.dateTo) {
            endDate = moment(this.data.dateTo, 'DD.MM.YYYY').toDate();
        }
        this.calendar.createEventInteractivelyWithOptions(this.data.title, this.data.location, cleanDescription, startDate, endDate, options)
            .then(() => {
                if(options.id !== undefined) {
                    this.eventsIds.push(options.id)
                }
                console.log('event created successfully')
            })
            .catch((err) => console.error('Error occured', err))
    }


1 Ответ

1 голос
/ 03 марта 2020

В первом блоке , а затем , если isPermitted - true, вы возвращаете true. Это означает, что обещание не будет следовать за другими связанными функциями.

Другое обещание можно связать, только когда вы возвращаете обещание. Вот некоторый фрагмент, чтобы прояснить идею.

checkCalendarUsePermission() {
    this.calendar.hasReadWritePermission()
        .then((isPermitted) => {
            if (isPermitted) {
                return new Promise((resolve, reject) => {
                  resolve(true);
                });
            }

            return this.calendar.requestReadWritePermission()
        })
        .then(() => {
            return this.checkIfEventExists();
        })
        .then((result) => {
            console.log('Result non exist', result);
            if(result){
                console.log(result);
                this.createNewEvent()
            }

        })
        .catch((err) => {
            console.log('Error occured while getting permission', err)
            this.appFunctionCtrl.presentToast('Error with accessing the write permission', 1500, 'bottom')
        })
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...