2

Googleファイアベースファーストアーストデータベース内には、コレクションに含まれるドキュメントの数などの集計データを収集したいと考えています。 Firestoreは集約クエリを提供していないので、コレクションに含まれるドキュメントの数を含むドキュメントがデータベースに追加されるたびにフィールドをインクリメントするクラウド関数を作成しようとしています。クラウドファンクション内のクラウドファイアストアドキュメントにアクセス

私が抱えている問題は、nodejsを使ってCloudstoreからFirestoreのドキュメントを取得する方法を理解できないことです。私は管理者のSDKとどのようなあなたは、このように持っているの設定

を私index.jsファイルの先頭に:ここで

は私がやっているものだ

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

その後、私のクラウド機能のために、私は次の操作を行います。

exports.createPost = functions.firestore 
    .document('posts/{post_id}') 
    .onCreate(event => { 

    // Get the post object 
    var post = event.data.data(); 

    var senderID = post["sender_id"]; // This is not null 

    // Access to user's document and the main ledger document 
    const userDocRef = admin.database().ref('/users').orderByChild("user_id").equalTo(senderID).once('value') 
    const ledgerDocRef = admin.database().ref('/posts/ledger').once('value'); 

    return Promise.all([userDocRef, ledgerDocRef]).then(snapshot => { 

     const user = snapshot[0].val(); 
     const ledger = snapshot[1].val(); 

     console.log("user => "+user); // Logs: user => null 
     console.log("ledger => "+ledger); // Logs: ledger => null 

     const userPostCount = user["user_name"]; 
     const globalPostCount = ledger["global_post_count"] + 1; 

     const userUpdate = user.update({"post_count" : userPostCount}); 
     const ledgerUpdate = ledger.update({"global_post_count" : globalPostCount}); 

     return Promise.all([userUpdate, ledgerUpdate]); 
    }); 
}); 

私はエラーで終わる:

TypeError: Cannot read property 'global_post_count' of null at Promise.all.then.snapshot

これは私の質問に何か問題があることを示していますが、何がわからないのですか。 userspostsは両方ともルートレベルのコレクションです。私は、オンラインで読んだ、私はそれが、私の効果が、それは注目に値すると思ったことはないと思う

Billing account not configured. External network is not accessible and quotas are severely limited.

から:

イムも言う警告を取得します。

助けてください。

答えて

6

あなたはFirestoreトリガを書いたが、その後のクエリのためのリアルタイムデータベースに達しているように見える:あなたのRTDBは空です

const userDocRef = admin.database().ref('/users').orderByChild("user_id").equalTo(senderID).once('value') 
const ledgerDocRef = admin.database().ref('/posts/ledger').once('value'); 

場合は、これらのクエリは、空に終わるでしょう。

Firestoreを照会するには、admin.database()の代わりにadmin.firestore()を使用する必要があります。 FirestoreにはRTDBよりもmostly different API(私がリンクしたばかりのCloud SDK経由)がありますが、いくつかの点で似ています。

+0

オハイオ州私は今それがかなり滑らかに見える。答えてくれてありがとう、私はこの1つの最後のカップルの時間の間、頭に頭を打っている。 –

関連する問題