Firestore 云功能无法在本地运行
Posted
技术标签:
【中文标题】Firestore 云功能无法在本地运行【英文标题】:Firestore Cloud Functions not working locally 【发布时间】:2019-10-06 21:28:32 【问题描述】:我已经创建了一个测试项目并确实根据文档配置了所有内容,但是当我运行该项目时,它没有按预期工作,在写这个问题之前,我做了谷歌并发现了一些重复的问题,但场景不同在每一个中,所以我假设这是不是重复的问题。
这是我的终端命令和输出:
functions ➤ npm run serve
> functions@ serve /Users/codecrash/Developments/crm-firestore/functions
> firebase serve --only functions
✔ functions: Using node@8 from host.
✔ functions: Emulator started at http://localhost:5000
i functions: Watching "/Users/codecrash/Developments/crm-firestore/functions" for Cloud Functions...
i Your code has been provided a "firebase-admin" instance.
i functions: HTTP trigger initialized at http://localhost:5000/demo-crm/us-central1/helloWorld
Ignoring trigger "onWrite" because the Cloud Firestore emulator is not running.
Ignoring trigger "onUpdate" because the Cloud Firestore emulator is not running.
i functions: Beginning execution of "helloWorld"
i Your code has been provided a "firebase-admin" instance.
i functions: Finished "helloWorld" in ~1s
我不确定,怎么了。 helloWorld 工作正常,但 onUpdate 和 onWrite 却不行。
我也尝试过运行以下命令,
functions ➤ firebase emulators:start
i Starting emulators: ["functions","firestore"]
✔ functions: Using node@8 from host.
✔ functions: Emulator started at http://localhost:5001
i firestore: Logging to firestore-debug.log
✔ firestore: Emulator started at http://127.0.0.1:8080
i firestore: For testing set FIREBASE_FIRESTORE_EMULATOR_ADDRESS=127.0.0.1:8080
i functions: Watching "/Users/codecrash/Developments/crm-firestore/functions" for Cloud Functions...
i Your code has been provided a "firebase-admin" instance.
i functions: HTTP trigger initialized at http://localhost:5001/demo-crm/us-central1/helloWorld
i functions: Setting up Cloud Firestore trigger "onWrite"
✔ functions: Trigger "onWrite" has been acknowledged by the Cloud Firestore emulator.
i functions: Setting up Cloud Firestore trigger "onUpdate"
✔ functions: Trigger "onUpdate" has been acknowledged by the Cloud Firestore emulator.
i functions: Beginning execution of "helloWorld"
i Your code has been provided a "firebase-admin" instance.
i functions: Finished "helloWorld" in ~1s
但 onUpdate 或 onWrite 触发器仍然不走运。 不知道我做错了什么。
这是我的代码库:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = functions.https.onRequest((request, response) =>
response.send("Hello from Firebase!");
);
exports.onWrite = functions.firestore.document('users/userId').onWrite((change, context) =>
const document = change.after.exists ? change.after.data() : null;
const oldDocument = change.before.data();
// perform desired operations ...
console.log('local: onWrite change:', change);
console.log('local: onWrite context:', context);
console.log('local: onWrite document:', document);
console.log('local: onWrite oldDocument:', oldDocument);
return Promise.resolve();
);
exports.onUpdate = functions.firestore.document('users/userId').onUpdate((change, context) =>
const document = change.after.exists ? change.after.data() : null;
const oldDocument = change.before.data();
// perform desired operations ...
console.log('local: onUpdate change:', change);
console.log('local: onUpdate context:', context);
console.log('local: onUpdate document:', document);
console.log('local: onUpdate oldDocument:', oldDocument);
return new Promise().then(() =>
return Promise.resolve();
).catch((error) =>
return Promise.reject('error:code_crash');
);
);
我在 env 变量中添加了键:
GOOGLE_APPLICATION_CREDENTIALS="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"
FIREBASE_CONFIG="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"
注意:当我将其部署在云功能上时,它运行良好。
谢谢。
【问题讨论】:
您使用的是哪个版本的 Firebase CLI?运行firebase --version
。
@DougStevenson 它是 6.10.0
您找到解决方案了吗?我在这里运行同样的问题
这里也一样,请帮忙
与 firebase --version 7.1.0 相同。它也不起作用。
【参考方案1】:
我不知道为什么文档中没有明确提到这一点,但显然需要让客户端 sdk 了解模拟器。有一个 API 将客户端 sdk 指向本地模拟器实例。这是我在初始化 firebase 应用程序后立即在项目中进行设置的方法。希望对您有所帮助。
firebase.initializeApp(CONFIG);
if (process.env.NODE_ENV === 'development')
firebase.functions().useFunctionsEmulator('http://localhost:5001');
更新
当这个问题被问到(并得到回答)时,firebase 本地模拟器正在制作中。所以文档不足。 Firebase 现在支持被称为Firebase Emulator Suite 的几乎所有服务(不是存储),并且有很好的文档。看看吧。
【讨论】:
让我试试这个解决方案。谢谢:) 嗨@ron4ex,我收到错误 firebase.functions() is not a function,在我的情况下,firebase 是管理员(npm firebase-admin 模块),知道吗? 我不确定你想要达到什么目的。上面的代码是从客户端 sdk 本地触发云功能。我怀疑不支持没有客户端应用程序的手动触发:***.com/questions/52416261/… 当我添加这个时,if (process.env.NODE_ENV === 'development') firebase.functions().useFunctionsEmulator('http://localhost:5001');
,webpack 抛出错误default.a.functions is not a function
。知道如何解决吗?以上是关于Firestore 云功能无法在本地运行的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 云功能与 Firestore 返回“已超过截止日期”
在云功能上使用 Firestore SDK 时出现错误:无法编码类型