热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用CloudFunctionsforFirebase更新firebase实时数据库中的值

如何解决《如何使用CloudFunctionsforFirebase更新firebase实时数据库中的值》经验,为你挑选了1个好方法。

我通过firebase文档使用Cloud Functions for Firebase更新了实时数据库中的值,但我无法理解.

我的数据库结构是

{   
 "user" : {
    "-KdD1f0ecmVXHZ3H3abZ" : {
      "email" : "ksdsd@sdsd.com",
      "first_name" : "John",
      "last_name" : "Smith",
      "isVerified" : false
    },
    "-KdG4iHEYjInv7ljBhgG" : {
      "email" : "superttest@sds213123d.com",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "isVerified" : false
    },
    "-KdGAZ8Ws6weXWo0essF" : {
      "email" : "superttest@sds213123d.com",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "isVerified" : false
    } 
}

我想使用数据库触发器云功能更新isVerified.我不知道如何使用云函数更新数据库值(语言:Node.JS)

当使用数据库触发器onWrite创建用户时,我编写了一个代码来自动更新用户键'isVerified'的值.我的代码是

const functiOns= require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.userVerification = functions.database.ref('/users/{pushId}')
    .onWrite(event => {
    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = event.data;

    if (event.data.previous.exists()) {
        return;
    }

    eventSnapshot.update({
        "isVerified": true
    });
});

但是当我部署代码并将用户添加到数据库时,云功能日志会显示以下错误

TypeError: eventSnapshot.child(...).update is not a function
    at exports.userVerification.functions.database.ref.onWrite.event (/user_code/index.js:10:36)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

Doug Stevens.. 12

您正尝试在DeltaSnapshot对象上调用update().在这种类型的对象上没有这样的方法.

var eventSnapshot = event.data;
eventSnapshot.update({
    "isVerified": true
});

event.data是DeltaSnapshot.如果要更改此对象所表示的更改位置的数据.使用其ref属性来获取Reference对象:

var ref = event.data.ref;
ref.update({
    "isVerified": true
});

此外,如果您正在函数中读取或写入数据库,则应始终返回一个Promise,指示更改何时完成:

return ref.update({
    "isVerified": true
});

我建议从评论中获取Frank的建议,并研究现有的示例代码和文档,以便更好地了解Cloud Functions的工作原理.



1> Doug Stevens..:

您正尝试在DeltaSnapshot对象上调用update().在这种类型的对象上没有这样的方法.

var eventSnapshot = event.data;
eventSnapshot.update({
    "isVerified": true
});

event.data是DeltaSnapshot.如果要更改此对象所表示的更改位置的数据.使用其ref属性来获取Reference对象:

var ref = event.data.ref;
ref.update({
    "isVerified": true
});

此外,如果您正在函数中读取或写入数据库,则应始终返回一个Promise,指示更改何时完成:

return ref.update({
    "isVerified": true
});

我建议从评论中获取Frank的建议,并研究现有的示例代码和文档,以便更好地了解Cloud Functions的工作原理.


推荐阅读
author-avatar
路过然后错过
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有