开发者问题收集

将 Firebase + Firestore 与 Deno 结合使用

2022-01-03
944

我正在尝试使用 firebase ,特别是 firestore ,与 Deno 一起使用,但在使用 v8v9 API 时都遇到了问题。

对于 v8 ,我按照 Deno 的本教程 进行操作,但收到错误 Property firestore does not exist on firebase.

import "https://deno.land/x/[email protected]/mod.ts";

import firebase from "https://cdn.skypack.dev/[email protected]/app";
import "https://cdn.skypack.dev/[email protected]/firestore";

const firebaseConfig = { ... };
firebase.initializeApp( firebaseConfig, 'app' );
const firebaseApp = firebase.app( 'app' );
const db = firebase.firestore( firebaseApp );

对于 firebase 版本 8.7.0 也会出现同样的错误。

对于 v9 ,我收到错误 Service firestore is not available

import "https://deno.land/x/[email protected]/mod.ts";

import { initializeApp } from "https://cdn.skypack.dev/[email protected]/app";
import { getFirestore } from "https://cdn.skypack.dev/[email protected]/firestore";

const firebaseConfig = { ... };
const firebaseApp = initializeApp( firebaseConfig );
const db = getFirestore( firebaseApp );

如果我没有通过 firebaseAppgetFirestore 调用时,我收到错误 No Firebase App '[DEFAULT]' has been created.

...
const db = getFirestore();

MRE

v8.js

import "https://deno.land/x/[email protected]/mod.ts";

import firebase from "https://cdn.skypack.dev/[email protected]/app";
import "https://cdn.skypack.dev/[email protected]/firestore";

const firebaseConfig = {
  apiKey: "apikey",
  authDomain: "pid.firebaseapp.com",
  projectId: "pid",
  storageBucket: "pid.appspot.com",
  messagingSenderId: "msid",
  appId: "aid",
  measurementId: "mid"
};

firebase.initializeApp( firebaseConfig, 'app' );
const firebaseApp = firebase.app( 'app' );
const db = firebase.firestore( firebaseApp );

使用 deno run v8.js 运行。

输出

> deno run v8.js

Property 'firestore' does not exist on type '{ __esModule: boolean; initializeApp: (options: any, rawConfig: any) => any; app: { (name2: any): any; App: any; }; registerVersion: (libraryKeyOrName: any, version2: any, variant: any) => void; ... 4 more ...; INTERNAL: { ...; }; }'.
const db = firebase.firestore( firebaseApp );

> deno run --no-check v8.js

Uncaught TypeError: Cannot read properties of null (reading 'INTERNAL')
    e3.INTERNAL.registerComponent(new Component("firestore", function(e4) {
       ^
    at https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/firestore.js:11912:8
    at I (https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/firestore.js:11916:4)
    at https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/firestore.js:11920:1

v9.js

import "https://deno.land/x/[email protected]/mod.ts";

import { initializeApp } from "https://cdn.skypack.dev/[email protected]/app";
import { getFirestore } from "https://cdn.skypack.dev/[email protected]/firestore";

const firebaseConfig = {
  apiKey: "apikey",
  authDomain: "pid.firebaseapp.com",
  projectId: "pid",
  storageBucket: "pid.appspot.com",
  messagingSenderId: "msid",
  appId: "aid",
  measurementId: "mid"
};

const firebaseApp = initializeApp( firebaseConfig );
const db = getFirestore( firebaseApp );

使用 deno run v8.js

输出

> deno run v9.js

Uncaught Error: Service firestore is not available
        throw Error(`Service ${this.name} is not available`);
              ^
    at Provider.getImmediate (https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/component.js:111:15)
    at Oa (https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/firestore.js:9969:39)
    at file:///home/brian/Downloads/firebase-test/mrc-v9.ts:17:12


> deno run --no-check v9.js

Uncaught Error: Service firestore is not available
        throw Error(`Service ${this.name} is not available`);
              ^
    at Provider.getImmediate (https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/component.js:111:15)
    at Oa (https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=imports/optimized/@firebase/firestore.js:9969:39)
    at file:///home/brian/Downloads/firebase-test/mrc-v9.ts:17:12
2个回答

正如@bicarlsen所说,该问题与firebase模块之间的兼容性有关。为此,他在Github上提出了一个 问题

2022-01-04

已在 GitHub 线程中发布了与此问题相关的解决方法 。您无需从 https://cdn.skypack.dev/ [email protected] /app 导入,而是可以从 https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js 导入,同样也可以替换其他模块 URL 中的 app

bicarlsen
2022-01-09