如何使用Cloud Functions for Firebase更新Firebase实时数据库中的值 [英] how to update a value in firebase realtime database using Cloud Functions for Firebase
问题描述
我通过firebase文档使用Cloud Functions for Firebase更新实时数据库中的值,但无法理解.
I went though the firebase docs for updating a value in realtime database using Cloud Functions for Firebase, but am not able to understand.
我的数据库结构是
{
"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)更新数据库值
I want to update the isVerified using database trigger cloud functions. I don't know how to update a database value using cloud functions (language : Node.JS)
当使用数据库触发器onWrite创建用户时,我编写了一个代码来自动更新用户键"isVerified"的值.我的代码是
I wrote a code to automatically update the value of the key 'isVerified' of a user, when the user is created by using database trigger onWrite. My code is
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
});
});
但是当我部署代码并将用户添加到数据库时,云功能日志显示以下错误
but when i deploy the code, and add a user to the database, the cloud function log shows the below error
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)
推荐答案
您正试图在DeltaSnapshot对象上调用update().在这种类型的对象上没有这种方法.
You are trying to call update() on a DeltaSnapshot object. There is no such method on that type of object.
var eventSnapshot = event.data;
eventSnapshot.update({
"isVerified": true
});
event.data
是 DeltaSnapshot .如果要在此对象表示的更改位置更改数据.使用其ref
属性获取参考对象的保持状态:
event.data
is a DeltaSnapshot. If you want to change the data at the location of the change represented by this object. Use its ref
property to get a hold of a Reference object:
var ref = event.data.ref;
ref.update({
"isVerified": true
});
此外,如果您要在函数中读取或写入数据库,则应始终返回承诺,指示更改何时完成:
Also, if you are reading or writing the database in a function, you should always return a Promise that indicates when the change is complete:
return ref.update({
"isVerified": true
});
我建议从评论中采纳弗兰克的建议,并研究现有的示例代码和文档以更好地了解Cloud Functions的工作原理.
I would recommend taking Frank's advice from the comments and study the existing sample code and documentation to better understand how Cloud Functions works.
这篇关于如何使用Cloud Functions for Firebase更新Firebase实时数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!