在本地测试 Cloud Functions 时,Cloud Firestore 模拟器未运行

car*_*ian 5 firebase google-cloud-functions google-cloud-firestore firebase-cli

我一直在尝试实现一些从 Cloud Firestore 读取数据的 Cloud Functions。我想在本地测试这些,但是我无法运行 Cloud Firestore 模拟器。我的假设是问题出在哪里,但可能在其他地方 - 如果是这种情况,请告诉我。

这是我的功能:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'

admin.initializeApp()

//Functions to fetch the documents in the user collection and send them to the client.
export const getUserDocs = functions.https.onRequest((request, response) => {
    admin.firestore().doc('/Users/{documentId}').get()
    .then(snapshot => {
        const data = snapshot.data()
        response.send(data)
    })
    .catch(error => {
        response.status(500).send(error)
    })
})

Run Code Online (Sandbox Code Playgroud)

这是我运行命令时得到的错误firebase serve --only functions

Carlo@Carlos-Air-2 functions % firebase serve --only functions
?  Your requested "node" version "8" doesn't match your global version "13"
?  functions: functions emulator started at http://localhost:5000
i  functions: Watching "/Users/Carlo/app/functions" for Cloud Functions...
?  functions[getUserSports]: http function initialized (http://localhost:5000/app/us-central1/getUserSports).
i  functions: Beginning execution of "getUserSports"
?  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
?  External network resource requested!
   - URL: "http://179.252.163.233/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
?  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
i  functions: Finished "getUserSports" in ~9s
Run Code Online (Sandbox Code Playgroud)

我试过从头开始,卸载并重新安装,但没有成功。我还在 YouTube 上观看了系列教程,但由于某种原因我的代码不起作用。

任何帮助是极大的赞赏。

Nel*_*les 5

尝试运行 Firebase 进行本地测试时出现以下错误:

Cloud Firestore 模拟器未运行,因此调用 Firestore 会影响生产。

我做错的是没有同时选择 Firestore 和 Functions(见图)。

然后有许多命令可用于启动本地测试:

  1. 火力基地服务
  2. firebase serve --only 功能
  3. firebase 模拟器:开始 --only=functions
  4. Firebase 模拟器:开始

最后一个(#4)为我解决了这个问题。

命令行输出的图像


Sim*_*mon 3

您的输出控制台中没有错误。

这只是一个警告,表明您没有在本地运行 Firestore,这是完全正常的,因为您运行命令--only functions,因此仅启动了该cloud functions部分(没有 firestore、规则等)