【问题标题】:node js function onWrite is not working properly in google cloud function节点js函数onWrite在谷歌云功能中无法正常工作
【发布时间】:2019-02-12 06:16:27
【问题描述】:

我有这个节点 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)));
  });

这是我的错误 thraceback

TypeError:无法读取未定义的属性“val” 在exports.indexlisting_algolia.functions.database.ref.onWrite (/user_code/index.js:807:40) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下一个(本机) 在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在 /var/tmp/worker/worker.js:733:24 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

第 807 行是这个函数

var firebaseObject = snapshot.data.val();

我做错了什么,我该如何解决?

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    您使用的是由 firebase-functions 模块公开的旧版本 API。新的要求你接受一个Change 对象,带有beforeafter 属性,作为onWrite 和onUpdate 触发器的第一个参数。这些属性将是 DataSnapshot 对象。您的代码当前需要一个 DataDeltaSnapshot,这是您在完整 1.0 版本之前的 beta 版本中获得的。现在已弃用。

    您可以阅读API changes in version 1.0 in the documentation

    有关示例,另请参阅documentation for database triggers

    你的函数应该看起来更像这样:

    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();
        })
    

    【讨论】:

      猜你喜欢
      • 2023-02-24
      • 2020-04-05
      • 2022-12-17
      • 2020-05-18
      • 2020-02-15
      • 2018-02-21
      • 2017-12-23
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多