[英]Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>'
[英]RxDB Angular2-cli 'Promise<void>' is not assignable to parameter of type 'Promise<any>'
所以我一直試圖讓RxDB在過去幾天工作,一個新的項目開始使用angular cli with command
ng new <Projectname>
之后
npm install rxdb
然后我在RxDB的例子中創建了一個服務我遇到了這條線路的麻煩
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;
})
)
當我嘗試使用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>'.)
第28行是我遵循RxDB文檔給出的示例
static db$: Observable<RxDatabase> = Observable.fromPromise(RxDB
.create('collectionD', adapters[useAdapter], 'myLongAndStupidPassword', true)
Vscode和ng服務都給出了相同的錯誤
到目前為止,這是我的代碼。 它創建了一個DB和一個集合:
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);
}
}
你能解釋一下你想在db$
observable中包裝什么嗎? 它是數據庫實例嗎? 收藏?
一個observable意味着發出值,我真的沒有看到將db實例或一個集合包裝在一個observable中的意義(好吧,它對於一個集合是有意義的,但是RxDB已經支持將一個查詢作為一個可觀察的本地公開)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.