登录  /  注册
无法使用从应用程序传递的 Firestore 对象从自定义 npm 包读取 Firestore DB
P粉064448449
P粉064448449 2023-09-10 19:49:11
[JavaScript讨论组]

我正在尝试编写自己的使用 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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号