在本地测试 Cloud Functions 时,Cloud Firestore 模拟器未运行
Posted
技术标签:
【中文标题】在本地测试 Cloud Functions 时,Cloud Firestore 模拟器未运行【英文标题】:Cloud Firestore emulator not running when testing Cloud Functions locally 【发布时间】:2020-07-28 05:24:16 【问题描述】:我一直在尝试实现一些从 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)
)
)
这是我在运行命令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
我尝试重新从头开始并卸载并重新安装,但没有成功。 我也在 YouTube 上观看了教程系列,但由于某种原因,我的代码无法正常工作。
非常感谢任何帮助。
【问题讨论】:
你可以用这个tianhaoz.com/eng/mobile/flutter/#test-with-firebase-emulator启动函数和数据库 【参考方案1】:我在尝试运行 firebase 进行本地测试时遇到以下错误:
Cloud Firestore 模拟器未运行,因此调用 Firestore 会影响生产。
我做错的是没有选择 Firestore 和 Functions(见图)。
那么有许多命令可用于启动本地测试:
-
firebase 服务
firebase serve --only 函数
firebase 模拟器:开始 --only=functions
firebase 模拟器:开始
最后一个(#4)为我解决了这个问题。
【讨论】:
【参考方案2】:您的输出控制台中没有错误。
这只是一个警告,说您没有在本地运行 Firestore,这是完全正常的,因为您使用 --only functions
运行命令,因此仅启动了 cloud functions
部分(没有 firestore、规则等)
【讨论】:
感谢您的快速回复。那么我该如何启动 Firestore、规则等呢?这是我必须添加到函数中的东西还是我必须在终端中运行的命令? 您可以通过从命令中删除 --only 函数来启动它们 我已经尝试过了,但我得到了相同的结果,函数在 promise 被拒绝时完成。知道可能出了什么问题吗? 这取决于错误,可能是规则问题。您可能应该用错误输出提出其他问题;) 那可能是规则问题以上是关于在本地测试 Cloud Functions 时,Cloud Firestore 模拟器未运行的主要内容,如果未能解决你的问题,请参考以下文章