Ionic 3 TypeScript错误 - 类型'void'上不存在属性'then'

时间:2018-03-15 20:25:41

标签: angular typescript ionic-framework

我是新的Ionic,当我收到错误TypeScript Error - Property 'then' does not exist on type 'void'时正在尝试教程。

它是一个简单的离子应用程序来保存笔记。这些音符保存在离子存储器中。

以下代码是我的服务页面代码

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage  } from '@ionic/storage';
import { Note  } from '../../models/note.model';
@Injectable()
export class NoteService {
  private notes: Note[] = [];
  private note: Note;
  constructor(public storage: Storage) {

  }
  saveNote(note: Note){
    note.created_at = Date.now();
    this.notes.push(note);
    this.storage.set('notes', this.notes);
  }
  getAllNotes(){
    return this.storage.get('notes').then(
        (notes) => {
            this.notes = notes == null ? [] : notes;
            return [...this.notes];
        }   
    )
  }
  deleteNote(created_at: number){
    this.notes = this.notes.filter((note) => {
        return note.created_at !== created_at
    })
    this.storage.set('notes', this.notes);
  }
  getNote(created_at: number){
    this.storage.get('notes').then((notes) => {
        this.note = [...notes].find(r => r.created_at === created_at);
        return this.note;
    }).catch(error => {
        console.log(error);
    });
  }
}

以下代码是我的home.ts文件

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AddNotePage } from '../add-note/add-note';
import { NoteService } from '../../providers/note-service/note-service';
import { Note  } from '../../models/note.model';
import { ViewNotePage } from '../view-note/view-note';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  private notes: Promise<Note[]>;
  private note: Note;
  constructor(public navCtrl: NavController, private noteService: NoteService) { }
  ionViewWillEnter(){
    this.notes = this.getAllNotes();
  }
  addNote(){
    this.navCtrl.push(AddNotePage);
  }
  getNote(created_at: number){
    this.noteService.getNote(created_at).then((n) => {
      this.note = n;
      this.navCtrl.push(ViewNotePage, { note: this.note });
    });
  }
  getAllNotes(){
    return this.noteService.getAllNotes();
  }
}

Here is the error displayed on the browser

2 个答案:

答案 0 :(得分:1)

NoteService.getNote未返回值。

也许试试这个:

getNote(created_at: number){
  return this.noteService.getNote(created_at).then((n) => {
    this.note = n;
    this.navCtrl.push(ViewNotePage, { note: this.note });
  });
}

答案 1 :(得分:1)

还有其他的东西,你的数据服务内部的函数返回对象而不是promise,所以你自己接收数据,所以不会那么:)

在你的家里。

  this.note = this.noteService.getNote(created_at);
  ///...nav ctrl push

但是在注意之前有推动的风险,没有注意它会导致错误。因此,您可以在服务中更改函数以返回promise,然后then将是正确的语法。

getNote(created_at: number){
    return new Promise((resolve, reject) => {
    this.storage.get('notes').then((notes) => {
        this.note = [...notes].find(r => r.created_at === created_at);
        resolve((this.note as any).data),
    }).catch(error => {
        console.log(error);
reject((error as any).error)
    });
}, error => reject((error as any).error)
  }