Javascript Firestore云功能在本地不工作
我创建了一个测试项目,并根据文档配置了所有内容,但当我运行该项目时,它并没有按照预期工作,在编写此问题之前,我在谷歌上搜索了一些重复的问题,但每个问题的场景都不一样,所以我假设这不是一个重复的问题 以下是我的终端命令和输出:Javascript Firestore云功能在本地不工作,javascript,firebase,google-cloud-firestore,google-cloud-functions,firebase-tools,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,Firebase Tools,我创建了一个测试项目,并根据文档配置了所有内容,但当我运行该项目时,它并没有按照预期工作,在编写此问题之前,我在谷歌上搜索了一些重复的问题,但每个问题的场景都不一样,所以我假设这不是一个重复的问题 以下是我的终端命令和输出: functions ➤ npm run serve > functions@ serve
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"
注意:当我在云功能上部署它时,它工作正常。
谢谢。我不知道为什么文档中没有明确提到这一点,但很明显,客户端sdk需要了解模拟器。存在将客户端sdk指向本地仿真器实例的方法。下面是我在初始化firebase应用程序后如何在项目中设置它。希望有帮助
firebase.initializeApp(配置);
if(process.env.NODE_env===‘development’){
firebase.functions().useFunctionsEmulator('http://localhost:5001');
}
更新
当这个问题被问到(并得到回答)时,firebase本地模拟器正在研制中。因此,文件不充分。Firebase现在支持几乎所有被称为的服务(而不是存储),并且有很好的文档。请查看。您使用的Firebase CLI版本是什么?运行
firebase--version
@DougStevenson它是6.10.0,您找到解决方案了吗?我在这里运行相同的问题相同,请在这里帮助firebase 7.1.0版的相同问题。它也不起作用。让我试试这个解决方案。谢谢:)Hi@ron4ex,我收到错误firebase.functions()不是函数,在我的例子中firebase是admin(npm firebase admin module),知道吗?我不知道你想实现什么。上面的代码是从客户端sdk本地触发云函数。我怀疑不支持手动触发客户端应用程序:当我添加此应用程序时,if(process.env.NODE_env==='development'){firebase.functions().useFunctionsEmulator('development')http://localhost:5001“);}
,网页包抛出错误default.a.functions不是函数
。知道怎么修吗?