無法使用從應用程式傳遞的 Firestore 物件從自訂 npm 套件讀取 Firestore DB
P粉064448449
P粉064448449 2023-09-10 19:49:11
0
1
471

我正在嘗試編寫自己的使用 Firestore 資料庫的 npm 套件。 我使用從應用程式傳遞的 Firestore 物件從套件中初始化類別:

import { firestoreTest } from 'my-package'

const app = initializeApp(firebaseConfig)
const db = getFirestore(app)
const test = new firestoreTest(db)

當我從套件中呼叫函數時 await test.getDoc('pathto/doc') 我收到錯誤: 未擷取(承諾中)FirebaseError:預期 collection() 的第一個參數是 CollectionReference、DocumentReference 或 FirebaseFirestore

我嘗試了初始化用於從 Firestore 讀取的 Firestore 物件的不同組合。 只有當我使用firebaseConfig 完全初始化包中的Firebase 應用程式時,我才能從Firestore 中讀取數據,但隨後我不會從父應用程式繼承身份驗證,只能讀取可公開訪問的集合。

我甚至嘗試將 doc(db, documentPath) 傳遞給包,然後出現錯誤,提示我可能發生的情況:類型與預期實例不符。您是否傳遞了來自不同 Firestore SDK 的引用?

因此,我認為使用我的套件中的 Firebase 會建立單獨的 Firebase SDK 實例。

我在我的應用程式和程式包中使用相同的 firebase 套件版本,也將套件 firebase 設定為對等依賴項。

我希望無縫使用在我的套件的主應用中初始化的相同 Firebase 物件。

有人可以幫助我了解我是否可以做一些事情來實現這一點嗎?

這是我的測試包中的程式碼。

export class firestoreTest {
  private db: Firestore
  private app: FirebaseApp
  private dbFromApp: Firestore
  private localApp: FirebaseApp
  private localDb: Firestore

  constructor(firebaseConfig: FirebaseOptions, app: FirebaseApp, db: Firestore) {
    this.db = db
    this.app = app

    this.dbFromApp = getFirestore(app)

    this.localApp = initializeApp(firebaseConfig)
    this.localDb = getFirestore(this.localApp)
  }

  public async getDoc(docPath: string, ref?: DocumentReference<DocumentData, DocumentData>) {
    /* Reads data, but without authenticated user context */
    /* Uncaught (in promise) FirebaseError: Missing or insufficient permissions. */
    // const data = await getDoc(doc(this.localDb, docPath).withConverter(converter<any>()))

    /* Uncaught (in promise) FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore */
    // const data = await getDoc(doc(this.dbFromApp, docPath).withConverter(converter<any>()))

    /* Uncaught (in promise) FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore */
    const data = await getDoc(doc(this.db, docPath).withConverter(converter<any>()))

    /* Uncaught (in promise) FirebaseError: Type does not match the expected instance. Did you pass a reference from a different Firestore SDK? */
    // const data = ref && (await getDoc(ref.withConverter(converter<any>())))

    console.log(data?.data())
  }
}

更新

我嘗試從本機來源安裝套件並使用符號連結到我的本機儲存庫,但這沒有幫助。

然後我將我的軟體包儲存庫推送到 GitHub 並使用 GitHub 儲存庫作為來源安裝軟體包,現在它可以工作了。

看起來在使用本機來源時,我的套件始終使用單獨的 Firebase 實例。 將整個包項目資料夾放入我的應用程式資料夾中並使用類似應用程式的部分包也沒有幫助。

從 GitHub 安裝並不是一個開發解決方案,因此我最終在我的應用程式專案中創建了資料夾,將所有套件檔案放置在其中,就像我的應用程式的一部分一樣。編輯它們後,我將複製到包存儲庫。

不知道是否有更好的解決方案。

更新

我發現套件 relative-deps 解決了我的開發問題。

P粉064448449
P粉064448449

全部回覆(1)
P粉080643975

由於原始發文者透過編輯問題回答了他/她的問題。發布此內容是為了提高社群的知名度:


npm 中,relative-deps 指的是功能,可讓您使用本機套件或工作空間內的相對檔案路徑定義依賴項。

此功能是在 npm 版本 7 中新增的。您可以直接引用專案中的其他套件或模組,而不是依賴套件名稱和 npm 註冊表。 p>

這使得開發更加容易,特別是對於使用 monorepos 或大量相互關聯的套件的項目。 npm 根據 package.json 檔案中指定的檔案路徑解析並安裝依賴項。


注意只有以 v7 及更高版本開頭的 npm 版本支援此功能。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!