You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Oracle JET项目中添加Firestore客户端SDK?

在Oracle JET项目中使用Google Cloud Firestore

可行性说明

可以在Oracle JET项目中使用Google Cloud Firestore客户端SDK,只需完成正确的依赖配置与初始化流程即可。

配置步骤

  1. 安装依赖
    你已执行的安装命令是正确的:
npm install firebase@10.12.2 --save
  1. 更新path_mapping.json
    Oracle JET通过path_mapping.json管理第三方库的路径映射,你当前的配置存在路径指向错误,需修改为以下内容:

首先添加firebase/app的映射(Firestore依赖Firebase核心模块):

"firebase/app": {
  "cdn": "3rdparty",
  "cwd": "node_modules/firebase/app",
  "debug": {
    "src": "dist/index.esm2017.js",
    "path": "libs/firebase/app/index.esm2017.js",
    "cdnPath": ""
  },
  "release": {
    "src": "dist/index.esm2017.min.js",
    "path": "libs/firebase/app/index.esm2017.min.js",
    "cdnPath": ""
  }
}

然后添加firebase/firestore的正确映射:

"firebase/firestore": {
  "cdn": "3rdparty",
  "cwd": "node_modules/firebase/firestore",
  "debug": {
    "src": "dist/index.esm2017.js",
    "path": "libs/firebase/firestore/index.esm2017.js",
    "cdnPath": ""
  },
  "release": {
    "src": "dist/index.esm2017.min.js",
    "path": "libs/firebase/firestore/index.esm2017.min.js",
    "cdnPath": ""
  }
}

配置说明:

  • cwd指向node_modules中对应模块的根目录
  • debug和release分别指定开发、生产环境的源文件及输出路径
  • 确保src路径与firebase@10.x版本的实际文件位置一致(esm文件位于dist目录下)
  1. 构建或启动项目
    配置完成后,运行以下命令让Oracle JET处理依赖并复制文件到指定路径:
ojet build
# 或启动开发服务器
ojet serve

使用示例

你的TS导入语句是正确的,补充初始化与使用示例:

import { initializeApp } from "firebase/app";
import { collection, getDocs, getFirestore } from "firebase/firestore";

// 替换为你的Firebase项目配置
const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_AUTH_DOMAIN",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_STORAGE_BUCKET",
  messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
  appId: "YOUR_APP_ID"
};

// 初始化Firebase应用
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

// 示例:获取指定集合的数据
async function fetchCollectionData() {
  try {
    const querySnapshot = await getDocs(collection(db, "your-collection-name"));
    querySnapshot.forEach((doc) => {
      console.log(`文档ID: ${doc.id}, 数据:`, doc.data());
    });
  } catch (error) {
    console.error("获取数据失败:", error);
  }
}

// 调用方法
fetchCollectionData();

内容的提问来源于stack exchange,提问作者Josh Sullivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:32:34