簡體   English   中英

RxDB Angular2-cli&#39;承諾 <void> &#39;不能賦值給&#39;Promise&#39;類型的參數 <any> “

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

vscode RxDB 所以我一直試圖讓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服務都給出了相同的錯誤

感謝您發送鏈接 ,我設法讓RxDB工作。

到目前為止,這是我的代碼。 它創建了一個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已經支持將一個查詢作為一個可觀察的本地公開)。

TypeScript型'承諾<void | object> ' 不可分配給類型 'Promise<object> ' 在 Promise.then()<div id="text_translate"><p> 我目前正在嘗試以 base64 格式實現緩存文檔的服務。 我希望這個服務從 sessionStorage 中獲取文檔,如果 sessionStorage 中沒有文檔,則從 IRequestService 中獲取它,然后將其保存到 sessionStorage。 我嘗試了這種方法,但出現類型錯誤</p><pre>Type 'Promise&lt;void | GetDocumentResult&gt;' is not assignable to type 'Promise&lt;GetDocumentResult&gt;'.</pre><p> 代碼如下所示:</p><pre> getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise&lt;GetDocumentResult&gt; { if (this.storageKey in sessionStorage) { const documentsMap: Map&lt;DocumentID, GetDocumentResult&gt; = new Map(JSON.parse(sessionStorage.getItem(this.storageKey).)) if (documentsMap.get(documentId).== undefined) { return new Promise(resolve =&gt; resolve(documentsMap,get(documentId).)) } } return requestService.getDocument(requestId, documentId) .then(value =&gt; {this.setDocument(documentId, value)}) }</pre><p> 我期待 Promise&lt;GetDocumentResult&gt;.then 的返回類型是 Promise&lt;GetDocumentResult&gt;,但由於某種我不明白的原因,它是 Promise&lt;void | 獲取文檔結果&gt;</p><p> 有人知道為什么會這樣嗎?</p><p> 謝謝</p></div></object></void>

[英]TypeScript Type 'Promise<void | Object>' is not assignable to type 'Promise<Object>' on Promise.then()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 打字稿:類型&#39;Promise &lt;{}&gt;&#39;不能分配給&#39;Promise&#39; <void> “ &#39;諾言 <void> &#39;不可分配給&#39;Promise <IListItem[]> angular2 TS2322類型&#39;Promise <void> &#39;不可分配給&#39;Promise <string> &#39;當給定字符串時 打字稿:輸入“承諾”<void> &#39; 不可分配給類型 &#39;void | 破壞者 諾言 <Employee> []無法分配給Employee []類型的參數 TypeScript型'承諾<void | object> ' 不可分配給類型 'Promise<object> ' 在 Promise.then()<div id="text_translate"><p> 我目前正在嘗試以 base64 格式實現緩存文檔的服務。 我希望這個服務從 sessionStorage 中獲取文檔,如果 sessionStorage 中沒有文檔,則從 IRequestService 中獲取它,然后將其保存到 sessionStorage。 我嘗試了這種方法,但出現類型錯誤</p><pre>Type 'Promise&lt;void | GetDocumentResult&gt;' is not assignable to type 'Promise&lt;GetDocumentResult&gt;'.</pre><p> 代碼如下所示:</p><pre> getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise&lt;GetDocumentResult&gt; { if (this.storageKey in sessionStorage) { const documentsMap: Map&lt;DocumentID, GetDocumentResult&gt; = new Map(JSON.parse(sessionStorage.getItem(this.storageKey).)) if (documentsMap.get(documentId).== undefined) { return new Promise(resolve =&gt; resolve(documentsMap,get(documentId).)) } } return requestService.getDocument(requestId, documentId) .then(value =&gt; {this.setDocument(documentId, value)}) }</pre><p> 我期待 Promise&lt;GetDocumentResult&gt;.then 的返回類型是 Promise&lt;GetDocumentResult&gt;,但由於某種我不明白的原因,它是 Promise&lt;void | 獲取文檔結果&gt;</p><p> 有人知道為什么會這樣嗎?</p><p> 謝謝</p></div></object></void> Promise 參數類型不可分配 'Promise 類型的參數<item | undefined> []' 不可分配給“SetStateAction”類型的參數<item[]> '</item[]></item> 打字稿:類型 &#39;Promise&lt;{}&gt;&#39; 不可分配給類型 反應 TS - 類型 '() =&gt; Promise<void> ' 不可分配給類型 'EventHandler<clickevent, boolean | void> '</clickevent,></void>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM