gpt4 book ai didi

javascript - Firestore 云功能无法在本地运行

转载 作者:可可西里 更新时间:2023-11-01 02:47:53 26 4
gpt4 key购买 nike

我已经创建了一个测试项目并根据文档配置了所有内容,但是当我运行该项目时,它没有按预期工作,在写这个问题之前,我做了谷歌并发现了一些重复的问题但是场景不同在每个问题中,所以我假设这不是重复的问题

这是我的终端命令和输出:

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');
});
});

我在环境变量中添加了键:

GOOGLE_APPLICATION_CREDENTIALS="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"
FIREBASE_CONFIG="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"

注意:当我将它部署在云函数上时,它工作正常。

谢谢。

最佳答案

我不知道为什么文档中没有明确提到这一点,但很明显客户端 sdk 需要了解模拟器。有一个 API将客户端 sdk 指向本地模拟器实例。以下是我在初始化 firebase 应用程序后立即在我的项目中进行设置的方法。希望对您有所帮助。

firebase.initializeApp(CONFIG);

if (process.env.NODE_ENV === 'development') {
firebase.functions().useFunctionsEmulator('http://localhost:5001');
}

更新

当这个问题被问到(并得到回答)时,firebase 本地模拟器正在制作中。所以文档不充分。 Firebase 现在支持被称为 Firebase Emulator Suite 的几乎所有服务(不是存储)并且有很好的文档。检查一下。

关于javascript - Firestore 云功能无法在本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com