javascript - 为浏览器代码导入 geofire-common 和 firestore

标签 javascript node.js google-cloud-firestore geofire vite

尝试在浏览器代码中使用 geofire-common 时我有点困惑。

一开始,我使用 NodeJS 进行了成功的尝试,它使用 geofire-common 引用 here in the Google documentation代码如下所示:

import { createRequire } from 'module'; // <------ trick to get a require function
const require = createRequire(import.meta.url);

const { initializeApp, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');

const serviceAccount = require('./serviceAccountKey.json');

initializeApp({
  credential: cert(serviceAccount),
});

const db = getFirestore();
const geofire = require('geofire-common'); //<---------- geofire-common

然后,我想从浏览器端完成这项工作......我偶然发现了 this example 。在这个例子中,我可以看到一个“require”函数,所以我猜它与老式的 commonJS 模块有关:

const geofire = require('geofire-common');

Require 不是标准 JavaScript API 的一部分,应该是 NodeJS 特定的(这有点奇怪,因为我认为示例是浏览器代码),所以我尝试使用 ES6 导入..

import {geofire} from 'geofire-common';

...没有成功:

The requested module '/node_modules/.vite/geofire-common.js?v=738ba8db' does not provide an export named 'geofire'

如果周围的一些 JS 专家愿意透露一些关于这 2 个软件包的信息,并帮助我正确导入 geofire(如果可能的话),我们将不胜感激。

最佳答案

我认为你可以使用

import * as geofire from 'geofire-common';

关于javascript - 为浏览器代码导入 geofire-common 和 firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70084254/

相关文章:

node.js - 如何使用 mongodb 存储/显示段落?

javascript - "TypeError: jsdom.jsdom is not a function"与 npm paper 模块中的 paper-node.js

java - 如何生成 PDF 发票并将其附加到电子邮件?

javascript - 如何在 firestore 中离线读取文档并写入(在线和离线)?

javascript - 获取 Cloud Firestore 数据库集合

javascript - 从另一个页面 JavaScript 中提取字符串

javascript - 如何通过 jquery 隐藏代码为 "."例如 1.1、1.2 的数据表行?

javascript - IE @font-face 与 JavaScript 不稳定?

javascript - 基于函数式编程的 JavaScript 应用程序是如何布局的?

javascript - jQuery:无控件