【问题标题】:connecting to firestore emulator from java admin sdk从 java admin sdk 连接到 Firestore 模拟器
【发布时间】:2020-12-26 02:39:24
【问题描述】:

我正在尝试从我的 java 应用程序内部连接到 firestore 模拟器,但没有成功。

FirebaseOptions options = FirebaseOptions.builder()
            .setCredentials(GoogleCredentials.getApplicationDefault())
            .setDatabaseUrl("localhost:8082")

            .setProjectId("xxx")
            .build();
    var app = FirebaseApp.initializeApp(options);

    Firestore db = FirestoreClient.getFirestore(app);
    final ApiFuture<WriteResult> update = db.collection("users").document().set(Map.of("test", "mest"));
    try {
        final WriteResult writeResult = update.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

这只是尝试连接到 firebase,而忽略数据库 url。

我查看了 node sdk,它有一个emulator 方法和数据库设置,但它们在 Java 端不存在。

【问题讨论】:

    标签: java google-cloud-firestore firebase-admin


    【解决方案1】:

    我在数据库链接方面遇到了类似的问题 你可以试试

    .setDatabaseUrl("http://localhost:8082")
    

    它对我有用。

    稍后编辑

    使用

    .setDatabaseUrl("http://localhost:8082?ns=fake-project-id")
    

    缺少最后一部分会在 websockets 上触发其他错误。

    【讨论】:

      【解决方案2】:

      根据documentation

      在设置 FIRESTORE_EMULATOR_HOST 环境变量时,Firebase Admin SDK 会自动连接到 Cloud Firestore 模拟器:

      导出 FIRESTORE_EMULATOR_HOST="localhost:8080"

      所以我猜你通过什么主机并不重要,只要这个环境存在?

      【讨论】:

        猜你喜欢
        • 2021-08-03
        • 2020-09-22
        • 2021-10-16
        • 2020-06-17
        • 2019-12-05
        • 2021-09-17
        • 1970-01-01
        • 2020-06-26
        • 2022-11-11
        相关资源
        最近更新 更多