Угловая форма не возвращает ошибку, если я возвращаю ошибку с асинхронным ожиданием.С AngularFire2 - PullRequest
0 голосов
/ 12 октября 2018

Этот код работает нормально

async save(input: HTMLInputElement) {
  const file = input.files.item(0);

  if (this.form.valid) {
    const data = this.form.getRawValue();
    await this.fireDatabase.object(`/Devices/${data.name}`).set(data);
    this.snackBar.open('Device successfully created', 'close', {
      duration: 2000
    });
    this.form.reset();
    input.value = '';
  }
}

Но если я проверяю с if условие

async save(input: HTMLInputElement) {
  const file = input.files.item(0);

  if (this.form.valid) {
    if (true) {
      this.snackBar.open('error', 'close', {
        duration: 2000
      });
    }
    const data = this.form.getRawValue();

    await this.fireDatabase.object(`/Devices/${data.name}`).set(data);

    this.snackBar.open('Device successfully created', 'close', {
      duration: 2000
    });
    this.form.reset();
    input.value = '';
  }
}

, тогда он не возвращает функцию.

Какэто исправить?

Я использую Angular и Angularfire

1 Ответ

0 голосов
/ 12 октября 2018

Попробуйте вернуть Promise оттуда, используя return Promise.resolve();

async save(input: HTMLInputElement) {
  const file = input.files.item(0);

  if (this.form.valid) {
    if (true) {
      this.snackBar.open('error', 'close', {
        duration: 2000
      });
    }
    const data = this.form.getRawValue();

    await this.fireDatabase.object(`/Devices/${data.name}`).set(data);

    this.snackBar.open('Device successfully created', 'close', {
      duration: 2000
    });
    this.form.reset();
    input.value = '';
  }
  else {
    return Promise.resolve(null);
  }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...