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

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

问题描述

我编写了此方法,该方法应该console.log触发的节点数据,但是出现错误.

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

这是错误:makeUppercaseTypeError: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)位于/worker/worker.js:825:24,位于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)

我做错了吗?

推荐答案

来自 docs :

事件数据现在为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 属性检索write之后的属性,或使用 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天全站免登陆