Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android firebase云函数错误类型错误:无法读取属性';儿童';未定义的_Android_Typescript_Firebase Realtime Database_Google Cloud Functions_Firebase Cloud Messaging - Fatal编程技术网

Android firebase云函数错误类型错误:无法读取属性';儿童';未定义的

Android firebase云函数错误类型错误:无法读取属性';儿童';未定义的,android,typescript,firebase-realtime-database,google-cloud-functions,firebase-cloud-messaging,Android,Typescript,Firebase Realtime Database,Google Cloud Functions,Firebase Cloud Messaging,我不熟悉firebase云功能,我的情况是每当数据库中发布新文章时,我都需要向我的应用程序用户显示通知 我的代码 var functions = require('firebase-functions'); var admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/art

我不熟悉firebase云功能,我的情况是每当数据库中发布新文章时,我都需要向我的应用程序用户显示通知

我的代码

var functions = require('firebase-functions');
var admin = require('firebase-admin');


admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/article/{articleId}')
.onWrite((event: { data: any; }) => {
        var eventSnapshot = event.data;
        var str1 = "Author is ";
        var str = str1.concat(eventSnapshot.child("author").val());
        console.log(str);

        var topic = "android";
        var payload = {
            data: {
                title: eventSnapshot.child("title").val(),
                author: eventSnapshot.child("author").val()
            }
        };


        return admin.messaging().sendToTopic(topic, payload)
            .then(function (response: any) {

                console.log("Successfully sent message:", response);
            })
            .catch(function (error: any) {
                console.log("Error sending message:", error);
            });
        });
这是我在Firebase中的数据库结构

来自logcat的完整Stacktrace在这里

TypeError: Cannot read property 'child' of undefined
    at exports.sendNotification.functions.database.ref.onWrite (/srv/lib/index.js:17:41)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

sendNotification
Function execution took 286 ms, finished with status: 'error'
TypeError:无法读取未定义的属性“child”
位于exports.sendNotification.functions.database.ref.onWrite(/srv/lib/index.js:17:41)
在cloudFunction(/srv/node_modules/firebase functions/lib/cloud functions.js:131:23)
at/worker/worker.js:825:24
在
在进程中。_tickDomainCallback(internal/process/next_tick.js:229:7)
发送通知
函数执行耗时286毫秒,完成状态为:“错误”
注意
onWrite()
处理程序接受一个对象,该对象在
属性之前有
之后有
,但在
数据之后没有
。我想这就是问题的原因,请参见
onWrite()
。如果要获取更新的数据,请使用
event.after.child('author')
而不是
event.data.child('author')

exports.sendNotification=functions.database.ref('/article/{articleId}').onWrite(change=>{
//删除数据时退出。
如果(!change.after.exists()){
返回null;
}
const author=change.after.child('author').val();
...
});

您正在使用旧版本的云函数API。如果您正在学习教程,则该教程已过时。我建议使用产品文档来了解它是如何工作的@SathishkumarG很高兴能帮助你!还请注意,如果文档被删除,则
change.after
可能不存在,因此最好检查它。我也更新了答案。