我已经创建了一个测试项目,并根据文档进行了配置,但是当我运行该项目时,它无法正常工作,在编写此问题之前,我做了Google并找到了一些重复的问题,但每个方案在每个方面都不同,所以我假设这是不是重复的问题。
这是我的终端命令和输出:
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的工作正常,但不努力和刻板。
我也通过运行contry命令,
尝试了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
,但仍然没有努力或写作触发器的运气。不知道我在做什么错。
这是我的代码库:
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"
注意:当我将其部署在云功能上时,它正常工作。
谢谢。
我不知道为什么在文档中没有清楚地提到这一点,但很明显,需要使客户sdk知道模拟器。有一个API可以将客户端SDK指向本地模拟器实例。这是我在初始化Firebase应用程序后立即将其设置在项目中的方式。希望会有所帮助。
firebase.initializeApp(CONFIG);
if (process.env.NODE_ENV === 'development') {
firebase.functions().useFunctionsEmulator('http://localhost:5001');
}
更新
当提出这个问题(并回答(时,正在制作Firebase本地模拟器。因此该文件不足。Firebase现在支持几乎所有的服务(而不是存储(在所谓的Firebase Emulator Suite中,并具有很好的文档。检查一下。