web-dev-qa-db-ja.com

Firestore Cloud Functionsがローカルで機能しない

私はテストプロジェクトを作成し、ドキュメントに従ってすべてを構成しましたが、プロジェクトを実行しているときに期待どおりに機能しません。この質問を書く前に、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は正常に機能していますが、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"

注:クラウド機能にデプロイしているときは問題なく動作しています。

ありがとうございます

14
Code_Crash

ドキュメントに埋もれて、ようやくこれに遭遇しました。また、Firestoreエミュレーターを指すための仕様も提供します。

https://firebase.google.com/docs/emulator-suite/connect_and_prototype?database=Firestore

0
Brock Klein