且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Cloud Functions for Firebase更新Firebase实时数据库中的值

更新时间:2023-10-23 13:14:22

您正试图在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.