Firestore Cloud功能无法在本地运行

Cod*_*ash 11 javascript firebase firebase-tools google-cloud-functions google-cloud-firestore

我已经创建了一个测试项目,并根据文档进行了所有配置,但是当我运行该项目时,它没有按预期运行,在编写此问题之前,我用过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
Run Code Online (Sandbox Code Playgroud)

我不确定,怎么了。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
Run Code Online (Sandbox Code Playgroud)

但仍然无法使用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');
    });
});
Run Code Online (Sandbox Code Playgroud)

我在env变量中添加了键:

GOOGLE_APPLICATION_CREDENTIALS="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"
FIREBASE_CONFIG="/Users/codecrash/Developments/crm-firestore/functions/certificate.json"
Run Code Online (Sandbox Code Playgroud)

注意:当我在云功能上部署它时,它工作正常。

谢谢。

ron*_*4ex 6

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

firebase.initializeApp(CONFIG);

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

更新

当提出(并回答)这个问题时,firebase 本地模拟器正在制作中。所以文档是不够的。Firebase 现在支持所谓的Firebase Emulator Suite 中的几乎所有服务(不是存储),并且有很好的文档。一探究竟。

  • @ron4ex,您好,我收到错误 firebase.functions() 不是函数,在我的情况下,firebase 是 admin(npm firebase-admin 模块),知道吗? (2认同)
  • 当我添加这个时,```if (process.env.NODE_ENV === 'development') { firebase.functions().useFunctionsEmulator('http://localhost:5001'); }```,webpack 抛出错误 `default.a.functions 不是函数`。知道如何修复它吗? (2认同)