簡體   English   中英

Firestore 雲功能無法在本地運行

[英]Firestore Cloud Functions not working locally

我創建了一個測試項目並根據文檔配置了所有內容,但是當我運行該項目時,它沒有按預期工作,在寫這個問題之前,我做了谷歌並發現了一些重復的問題,但每個問題的場景都不同,所以我假設這不是一個重復的問題

這是我的終端命令和輸出:

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 需要知道模擬器。 有一個API可以將客戶端 sdk 指向本地模擬器實例。 這是我在初始化 firebase 應用程序后立即在我的項目中設置它的方法。 希望有幫助。

firebase.initializeApp(CONFIG);

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

更新

當提出(並回答)這個問題時,firebase 本地模擬器正在制作中。 所以文檔是不夠的。 Firebase 現在支持所謂的Firebase Emulator Suite 中的幾乎所有服務(不是存儲),並且有很好的文檔。 一探究竟。

我已經創建了一個測試項目,並根據文檔進行了所有配置,但是當我運行該項目時,它沒有按預期運行,在編寫此問題之前,我用過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');
    });
});

我在env變量中添加了鍵:

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

注意:當我在雲功能上部署它時,它工作正常。

謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM