Firebase snapshot.val() 不是函数 [英] Firebase snapshot.val() is not a function

查看:21
本文介绍了Firebase snapshot.val() 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个方法,它应该控制台记录触发的节点数据,但我得到了错误.

I writed this method which should console.log the trigered node data, but i get error.

这是我试过的"

exports.makeUppercase = functions.database
  .ref('/users/{userId}/matches')
  .onWrite((snapshot, context) => {
    // Grab the current value of what was written to the Realtime Database.
    //const original = snapshot.val();
    console.log('OnWrite works' + snapshot.after.val());
    // const uppercase = original.toUpperCase();
    // You must return a Promise when performing asynchronous tasks inside a Functions such as
    // writing to the Firebase Realtime Database.
    // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
    return null;
  });

这是错误:make大写TypeError:snapshot.val 不是位于 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions) 的 export.makeUppercase.functions.database.ref.onWrite (/srv/index.js:49:44) 的函数.js:131:23) at/worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

This is the error: makeUppercase TypeError: snapshot.val is not a function at exports.makeUppercase.functions.database.ref.onWrite (/srv/index.js:49:44) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

我是不是做错了什么?

推荐答案

来自 文档:

事件数据现在是 DataSnapshot.

Event data now a DataSnapshot.

在早期版本中,event.data 是一个 DeltaSnapshot;从 v 1.0 开始,它是一个 DataSnapshot.

In earlier releases, event.data was a DeltaSnapshot; from v 1.0 onward it is a DataSnapshot.

对于 onWrite 和 onUpdate 事件,data 参数有 before 和 after 字段.其中每一个都是一个 DataSnapshot,其方法与 admin.database.DataSnapshot 中可用的方法相同.

For onWrite and onUpdate events, the data parameter has before and after fields. Each of these is a DataSnapshot with the same methods available in admin.database.DataSnapshot.

例如:

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
  const beforeData = change.before.val(); // data before the write
  const afterData = change.after.val(); // data after the write
});

因此,在您的代码中,您需要使用 after 属性来检索写入后或 before 属性:

Therefore in your code, you need to either use the after property to retrieve the after the write or before property:

const original = snapshot.after.val();

这篇关于Firebase snapshot.val() 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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