节点js函数onWrite在Google Cloud函数中无法正常工作 [英] node js function onWrite is not working properly in google cloud function

查看:76
本文介绍了节点js函数onWrite在Google Cloud函数中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个节点js函数,一旦对节点列表进行添加/更新/删除,便会尝试更新Algolia索引

I have this node js function that attempts to update Algolia index once a add/update/delete is done to node Listings

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:无法读取未定义的属性"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)

TypeError: Cannot read property 'val' of undefined at exports.indexlisting_algolia.functions.database.ref.onWrite (/user_code/index.js:807:40) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:733:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

第807行是此功能

var firebaseObject = snapshot.data.val();

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

what am I doing wrongly and how can I fix this?

推荐答案

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

You're using an old version of the API exposed by the firebase-functions module. The new one requires that you accept a Change object, with before and after attributes, as the first parameter of onWrite and onUpdate triggers. Those attributes will be DataSnapshot objets. Your code is currently expecting a DataDeltaSnapshot, which is what you got in the beta version before the full 1.0 release. This is now deprecated.

您可以在文档中了解有关1.0版中 API更改的信息.

You can read about the API changes in version 1.0 in the documentation.

请参见有关数据库触发器的文档以获取示例.

您的函数应更像这样:

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

这篇关于节点js函数onWrite在Google Cloud函数中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆