簡體   English   中英

節點js函數onWrite在Google Cloud函數中無法正常工作

[英]node js function onWrite is not working properly in google cloud function

我有這個節點js函數,一旦對節點列表進行添加/更新/刪除,它就會嘗試更新Algolia索引

exports.indexlisting_algolia = 
    functions.database.ref('/Listings/{listingId}').onWrite((snapshot, context) => {
   const index = algolia.initIndex('Listings');
   // var firebaseObject = snapshot.data;
   var firebaseObject = snapshot.data.val();
   console.log("test ",firebaseObject)

   firebaseObject.objectID = context.params.listingId;


  return index.saveObject(firebaseObject).then(
  () => 
   snapshot.data.adminRef.parent.child('last_index_timestamp').set(
      Date.parse(event.timestamp)));
  });

這是我的錯誤回擊

TypeError:無法在對象處讀取export.indexlisting_algolia.functions.database.ref.onWrite(/user_code/index.js:807:40)上未定義的屬性“ val”。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在(本機)在__awaiter的/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)位於/ var /處的cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在process._tickDomainCallback處的tmp / worker / worker.js:733:24(內部/process/next_tick.js:135:7)

807行就是這個功能

var firebaseObject = snapshot.data.val();

我做錯了什么,該如何解決?

您正在使用firebase-functions模塊公開的API的舊版本。 新的要求您接受具有屬性beforeafterChange對象作為onWrite和onUpdate觸發器的第一個參數。 這些屬性將是DataSnapshot對象。 您的代碼當前期望使用DataDeltaSnapshot,這是您在完整1.0版本之前的beta版本中獲得的內容。 現在已棄用。

您可以在文檔中了解1.0版中API更改

另請參見數據庫觸發器文檔以獲取示例。

您的函數應如下所示:

exports.indexlisting_algolia = 
    functions.database.ref('/Listings/{listingId}')
    .onWrite((change, context) => {
        const before = change.before;  // snapshot before the update
        const after = change.after;    // snapshot after the update
        const before_data = before.val();
        const afater_data = after.val();
    })

暫無
暫無

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

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