【问题标题】:ERROR Error: Uncaught (in promise): TypeError: Promise.resolve is not a function TypeError: Promise.resolve is not a functionERROR 错误:未捕获(在承诺中):TypeError:Promise.resolve 不是函数 TypeError:Promise.resolve 不是函数
【发布时间】:2020-05-19 01:31:49
【问题描述】:

我是 ionic 4 的初学者,当我调用我的服务时出现此错误

ERROR Error: Uncaught (in promise): TypeError: Promise.resolve is not 函数类型错误:Promise.resolve 不是函数

我的服务:


import { Injectable } from '@angular/core';
import PouchDB from 'pouchdb';
import PouchdbFind from 'pouchdb-find';
import RelationalPouch from 'relational-pouch';

@Injectable({
  providedIn: 'root'
})
export class PouchdbService {
  public remoteDB: any;
  public localDB: any;

  constructor() { 
    PouchDB.plugin(RelationalPouch);
    PouchDB.plugin(PouchdbFind);
    this.localDB = new PouchDB('couture');
    this.createDBSchema();
  }


  public createRelationalDoc(doc){
    return this.localDB.rel.save(doc.type, doc);
  }

  public updateRelationalDoc(doc){
    return this.localDB.rel.save(doc.type, doc);
  }

  public deleteRelationalDocDefinitivement(doc){
    return this.localDB.rel.del(doc.type, doc);
  }

  public findRelationalDocByID(type, id){
    return this.localDB.rel.find(type,id);
  }

  public findRelationalDocByTypeAndID(type, id){
    return this.localDB.rel.find(type,id);
  }

  public existRelationalDocByTypeAndID(type, id){
    return this.localDB.rel.find(type,id).then((res) => {
      return true
    }).catch((err) => {
      return false;
    });
  }


  public findAllRelationalDocByType(type){
    return this.localDB.rel.find(type);
  }

  public findRelationalDocByTypeAndOptions(type, options){
    return this.localDB.rel.find(type, options);
  }

  public findRelationalDocHasMany(type, belongsToKey, belongsToId){
    return this.localDB.rel.findHasMany(type, belongsToKey, belongsToId);
  }





  createDBSchema(){

    this.localDB.setSchema([

      {
        singular: 'client',
        plural: 'clients',
        relations: {
          'user': {belongsTo: 'user'}
        }

      },
      {
        singular: 'tache',
        plural: 'taches',
        relations: {
          'client': {belongsTo: 'client'},
          'user': {belongsTo: 'user'}
        }
      },
      {
        singular: 'user',
        plural: 'users'
      }
    ]);
  }

还有我的页面:

import { PouchdbService } from 'src/app/services/pouchdb.service';

@Component({
  selector: 'app-user',
  templateUrl: 'user.page.html',
  styleUrls: ['user.page.scss']
})
export class UserPage {

  public header:string;
  public user:any;
  public users:any;
  public type:any="user";

  constructor( private pouchdbService:PouchdbService) {
      this.getAll();

  }

  ngOnInit() {

  }

  ionViewWillEnter(){

  }


  getAll(){
this.pouchdbService.findAllRelationalDocByType(this.type)
    .then((data) => {
      console.log(data);
    }).catch((err) => {
      console.log(err);
    }); 
  }
}

当我调用getAll() 方法时出现以下错误:

Error: Uncaught (in promise): TypeError: Promise.resolve is not a function TypeError: Promise.resolve 不是函数

【问题讨论】:

    标签: javascript angular promise ionic4


    【解决方案1】:

    您需要将函数标记为异步并返回正确类型的承诺。

    async findAllRelationalDocByType(type) : Promise<*insert type here*>{
            return this.localDB.rel.find(type);
          }
    

    然后将您的调用标记为异步并等待结果

    async getAll(){
        await this.pouchdbService.findAllRelationalDocByType(this.type)
        .then((data) => {
          console.log(data);
        }).catch((err) => {
          console.log(err);
        }); 
      }
    }
    

    希望对你有帮助

    【讨论】:

    • 谢谢,但我总是遇到 TypeError: Promise.resolve is not a function as error
    • 我还将调用从构造函数移到 ionViewDidEnter()
    • TypeError: Promise.resolve is not a function at Object.find (index.js:429) at PouchdbService. (pouchdb.service.ts:65) at Generator.next () 在 tslib.es6.js:73 在新 ZoneAwarePromise (zone-evergreen.js:876) 在 Module.__awaiter (tslib.es6.js:69) 在 PouchdbService.findAllRelationalDocByType (pouchdb.service.ts:64) 在 UserPage . (user.page.ts:41) at Generator.next () at tslib.es6.js:73
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2018-11-05
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多