简体   繁体   中英

RxDB Angular2-cli 'Promise<void>' is not assignable to parameter of type 'Promise<any>'

vscode RxDB So I have been trying to make RxDB work for the past few days with a new project started with angular cli with command

ng new <Projectname>

after that

npm install rxdb

then I create a service as its in the example of the RxDB I am having trouble with this line

export class DatabaseService {
      static db$: Observable<RxDatabase> = Observable.fromPromise(RxDB
       .create('collectionD', adapters[useAdapter], 'myLongAndStupidPassword', true)
.then(db => {
        console.log('created Database');
        window['db'] = db;

        db.waitForLeadership()
            .then(() => {
                console.log('isLeader Now');
                document.title = '♛ ' + document.title;
            });
        console.log('DatabaseService: create Collections');
        const fns = collections
            .map(col => db.collection(col.name, col.schema));
        return Promise.all(fns)

            .then((cols) => {
                collections.map(col => col.dbCol = cols.shift());
                return db;
            });
    })
    // hooks
    .then( db => {
        db.collections.hero.preInsert( docObj => {
            const color = docObj.color;
            return db.collections.hero.findOne( {color} ).exec()
            .then( has => {
                if ( has != null ) {
                  alert( 'another hero already has the color ' + color );
                  throw new Error( 'color already there' );
                }
                return db;
            });
        });
    })

    .then( db => {
        console.log('created collections');
        return db;
    })
)

I get this error when i try to run it with webpack

ERROR in G:/projects/src/app/services/database.service.ts (28,62): Argument of type 'Promise<void>' is not assignable to parameter of type 'Promise<any>'.  Property '[Symbol.toStringTag]' is missing in type 'Promise<void>'.)

The line 28 is this one I am following the example given by the RxDB docs

  static db$: Observable<RxDatabase> = Observable.fromPromise(RxDB
   .create('collectionD', adapters[useAdapter], 'myLongAndStupidPassword', true)

The Vscode and ng serve both give the same error

Thanks to the link you sent , I managed to get RxDB to work.

Here's my code so far. It creates a DB and a collection:

import {Component} from '@angular/core';

const _Promise = Promise;
const RxDB = require('rxdb');
RxDB.plugin(require('pouchdb-adapter-websql'));
Promise = _Promise;

const heroSchema = { ... };

@Component({
  selector: 'rxdb',
  template: `RxdbComponent`
})
export class RxdbComponent {
  constructor() {
    RxdbComponent.createDb()
      .then(db => RxdbComponent.createCollection(db))
      .then(coll => console.log(coll));
  }

  static createDb() {
    return RxDB.create('tempDB', 'websql');
  }

  static createCollection(db: any) {
    return db.collection('users', heroSchema);
  }
}

Can you explain what you would like to wrap in your db$ observable? Is it the database instance? The collection?

An observable is meant to emit values and I don't really see the point of wrapping either the db instance or a collection inside an observable (well, it would make sense for a collection, but RxDB already supports exposing a query as an observable natively).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM