0

私はレガシーアプリケーションを使用してレコードを作成するたびにFireTalkバックエンドアーキテクチャを変更し、 )。しかし、私は私のログに次のエラーを取得しています:ここでFirebaseタイプクラウド機能のエラー - プロパティを読み取ることができません

TypeError: Cannot read property 'update' of undefined 
    at exports.makeNewComment.functions.database.ref.onWrite.event (/user_code/index.js:14:92) 
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20 
    at process._tickDomainCallback (internal/process/next_tick.js:129:7) 

は、問題のスクリプトは次のとおりです。

//required modules 
var functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 

// Listens for new comments added to /comments/ and adds it to /post-comments/ 

exports.makeNewComment = functions.database.ref('comments/{commentId}').onWrite(event => { 
    // Grab the current value of what was written to the Realtime Database. 
    const commentId = event.params.commentId; 
    const comment = event.data.val(); 
    // You must return a Promise when performing asynchronous tasks inside a Functions such as 
    // writing to the Firebase Realtime Database. 
    //return event.data.ref.parent.child('post-comments').set(comment); 
    return functions.database.ref('post-comments/' + comment['postID'] + '/' + commentId).update(comment).then(url => { 
    return functions.database.ref('user-comments/' + comment['postedBy'] + '/' + commentId).update(comment); 
    }); 
}); 

//initialize 
admin.initializeApp(functions.config().firebase); 

ありがとう!

答えて

1

Dougの答えに基づいて、functions.database.refevent.data.ref.rootに置き換えることができます。

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

exports.makeNewComment = functions.database.ref('comments/{commentId}').onWrite(event => { 

    const commentId = event.params.commentId; 
    const comment = event.data.val(); 

    return event.data.ref.root.child('post-comments/' + comment['postID'] + '/' + commentId).update(comment).then(url => { 
    return event.data.ref.root.child('user-comments/' + comment['postedBy'] + '/' + commentId).update(comment); 
    }); 
}); 

admin.initializeApp(functions.config().firebase); 
4

関数の途中でfunctions.database.ref()を使用して、データベースのどこかに参照を取得することはできません。これは、新しいクラウド機能を定義するためのものです。

データベースのどこかに参照したい場合は、event.data.refまたはevent.data.adminRefを使用して、イベントがトリガーされた場所を参照することができます。その後、rootプロパティを使用して、データベース内の他の場所への新しい参照を再構築することができます。または、adminオブジェクトを使用して新しいrefを作成することもできます。

sample codeを見れば、どのように動作するのかを知ることができます。

関連する問題