1
// The Cloud Functions for Firebase SDK to create Cloud Functions and 
setup triggers. 
const functions = require('firebase-functions'); 

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // Get the field values of what I am working with 
    const oldGiven = event.data.previous.data()['given']; 
    const newGiven = event.data.data()['given']; 

    // Get the cardID to make sure that is there 
    const cardID = event.params.cardsId; 

    // An array to go through 
    const give_profiles = event.data.data()['given_profiles']; 

    // error cardDatatwo is returned as undefined 
    const cardDatatwo = newGiven.parent; 

    // error cardDatathree is returned as undefined 
    const cardDatathree = event.data.ref.root 

    // // error cardDatafour cannot read propoerty of undefined 
    // const cardDatafour = cardDatathree.child('Profiles/{profileId}/cards/{cardsId}') 

    // error cardDatafive 'The value of cardfive is DocumentReference... 
    const cardDatafive = event.data.ref.firestore.doc('Profiles/{profileId}/cards/{cardsId}'); 

    // Check that the values have changed 
    if (newGiven == oldGiven) return; 

    if (newGiven !== undefined) { 

     console.log('The old value of given is', oldGiven); 
     console.log('The new value of given is', newGiven); 
     console.log('The value of the card is', cardID); 
     console.log('The value of the cardtwo is', cardDatatwo); 
     console.log('The value of the cardthree is', cardDatathree); 
     // console.log('The value of the cardfour is', cardDatafour); 
     console.log('The value of the cardfive is', cardDatafive); 

     for (var profile of give_profiles) { 
      console.log(profile); 
     }; 
     return; 
    } 
    return console.log("No given value"); 
}); 

Firestoreがクラウド機能で動作するためのルートを取得するのが非常に困難です。それはもちろん、異なって動作します。クラウドファンクションとFirestoreがFirebaseのルートになっていない

私は、さらに更新されたonUpdateが実行された後、ルートに向かってパスを取得しようとしています。

.parentはもちろんの functions.database.refは、それがリアルタイムデータベース だと動作しないと( firebase.firestoreを使用することはできません動作しませんが)また、ノード とevent.data.ref.firestoreに動作していません.docは定義されていません。

私は確かにすべてのオプションを通過しました。

希望すると助かります。

+0

あなたはどこまで持っていて、どこに 'parent'を実装する必要があるかを示すコードをいくつか共有できますか?これは 'DocumentSnapshot'のリスナーコールバック内にあるでしょうか? – Grimthorr

+0

Firestore APIを使用できるようにするには、コードを示してください。また、firebase-adminモジュールを最新バージョンに更新する必要があります。返信の場合は –

答えて

1

the documentationによると、あなたはこのように、firestoreを経由してコレクションにアクセスすることができます

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // other code 

    const ref = event.data.ref.firestore.doc('your/path/here'); 
    return ref.set({foo: 'bar'}).then(res => { 
     console.log('Document written'); 
    }); 

}); 
あなたがアクセスしようとしているデータベースのどんな部分へのパスを構築するために firestoreを使用することができます

event.data.ref.parentを次のように使用することもできます。

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // other code 

    const parentref = event.data.ref.parent; 
    const grandparentref = parentref.parent; // gets to cardsId 
    return grandparentref.set({foo: 'bar'}).then(res => { 
     console.log('Document written'); 
    }); 

}); 
+0

TYです。申し訳ありませんが、私もそれを入れなければなりません。私は前にそれを試しました。まだ定義されていません。今私はすべての可能な順列を試みたと確信しています。エラーメッセージと共にそのコードを追加するようにコードを変更しました。 – woisme

+0

私は自分で試したので動作します。しかし、リファレンス 'const cardDatafive = event.data.ref.firestore.doc( 'Profiles/{profileId}/cards/{cardsId}');は中括弧のために動作しません。データの書き込みや読み取りを行う特定のパスにワイルドカードを使用することはできません。 –

+0

私は 'event.data.ref.parent'を使用した例を示すために私の答えを更新しました。これは、書き込みが発生したパスの上位にアクセスするために働きます。 –

関連する問題