【问题标题】:Firebase emulator wont runFirebase 模拟器无法运行
【发布时间】:2021-03-17 12:53:49
【问题描述】:

我正在使用 Firebase 模拟器来测试我的云功能。我要执行的代码是:

import * as functions from "firebase-functions";

const fetch = require('node-fetch');

export const newZipCode = functions.database.ref('/zipCodes/{zipCode}').onCreate(async (snapshot, context) => {
    const api = await fetch(`https://api.papapi.se/lite/?query=${context.params.zipCode}&format=json&apikey=xxx`);
    const json = await api.json();

    console.log(context.params.zipCode);

    if (api.ok) {
        snapshot.ref.update({
            latitude: json.results[0].latitude,
            longitude: json.results[0].longitude
        });
    } else {
        console.log(${api.status}));
    }
});

但是,当我尝试运行 Firestore 和 Functions 模拟器时,我收到以下错误消息:

"缺少预期的 firebase 配置值 databaseURL,config 实际上是{"storageBucket":"xxxx-yyyy.appspot.com","projectId":"xxxx-yyyy"}"

Cloud Functions 和 Firestore 仿真器不应该能够直接进行通信吗?如果没有,需要做什么才能让模拟器运行?仅供参考,我正在运行 Node.js 14 和 Firebase 版本 9.6.1。

【问题讨论】:

  • 如何启动模拟器?
  • @fjc "firebase emulators:start --only functions,firestore" 或者只是 "firebase emulators:start"

标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-tools


【解决方案1】:

问题在于,当您应该启动数据库模拟器时,您正在启动 Firestore 模拟器,因为您使用的是Realtime Database onCreate() event handler

由于您正在启动 Firestore 模拟器,因此您可能没有在 Firebase 控制台中创建实时数据库,因此您会收到 "Missing expected firebase config value databaseURL..." 错误。您可以按照public documentation.的步骤创建一个

或者如果您想使用Cloud Firestore,您需要修改您的代码以使用Firestore onCreate() event handler

import * as functions from "firebase-functions";

const fetch = require('node-fetch');

export const newZipCode = functions.firestore.document('/zipCodes/{zipCode}').onCreate(async (snapshot, context) => {
    const api = await fetch(`https://api.papapi.se/lite/?query=${context.params.zipCode}&format=json&apikey=xxx`);
    const json = await api.json();

    console.log(context.params.zipCode);

    if (api.ok) {
        snapshot.ref.update({
            latitude: json.results[0].latitude,
            longitude: json.results[0].longitude
        });
    } else {
        console.log(${api.status}));
    }
});

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 2022-01-25
    • 2019-02-14
    • 2021-10-04
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多