簡體   English   中英

使用回調返回的Firestore onWrite觸發器返回未定義

[英]Firestore onWrite trigger using callback return undefined

我試圖在雲功能上添加觸發功能。

當文檔更改時,我想執行一些工作。 我已經有工作要做的代碼在另一個文件中(我將工作分成文件,這樣對我來說更容易了),所以我有一個執行工作的函數,完成后將調用回調。

這是我的index.js的樣子:

// requires...

// Express, i also use express for simulating an API
const app = express()


// So there is my Listener 

const listenerForChange = functions.firestore
    .document('myPath/documentName')
    .onWrite((change, context) => {

      const document = change.after.exists ? change.after.data() : null;

      if (document != null) {
        const oldDocument = change.before.data();
        const newDocument = change.after.data()

        // Here i call my function that is in the worker-listeners.js
        listenerWorker.performStuff(newDocument, function() {
            return 0
        })
      }
      else {
        console.error("Listener for classic was triggered but doc does not exist")
        return 0
      }

    });

const api = functions.https.onRequest(app)
module.exports = {
  api, 
  listenerForChange
}
...

有我的listenerWorker.js:

module.exports = {
    performStuff: function(data, complete) {
      performStuff(data, complete)
    }
}; 


function performStuff(data, complete) {

 // do stuff here ... 

  complete()

} 

問題是我在Firebase控制台中總是出現錯誤,說: Function returned undefined, expected Promise or value

即使我在工人內部不做任何事情並盡快調用回調,我也會收到錯誤消息。

因此,我了解功能需要響應,承諾或價值。 但這就像我不在范圍內了,但我不想在工作完成之前返回!

有什么幫助嗎? 感謝大伙們

您執行的所有異步工作都應由Promise表示。 如果您有一個執行異步工作的實用程序對象,則該對象應返回一個Promise,以便調用者可以決定下一步要做什么。 現在,您只使用回調,這會使事情變得比必要的要困難得多(因為您必須“保證”這些回調)。

只要在任何地方使用Promise,就會更加清楚您的函數應如何工作。

暫無
暫無

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

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