0

Firestoreコレクション(「番組」)にフィールドから引き出しているフィールド(「artistName」)を作成しようとしています"名前")を別のFirestoreコレクション(「アーティスト」)に追加します。 「ショー」コレクションには、「アーティスト」コレクション内のドキュメントを指し示す参照フィールド(「アーティスト」)があります。フィールドを作成するには、Google Cloud機能を使用します。ここに私のコードです:Google Cloudの機能を使用して別のFirestoreコレクションで参照されているフィールドからFirestoreコレクションのフィールドを作成する

exports.addReferenceDataToCollection = functions.firestore 
    .document('shows/{showId}').onWrite(event => { 
    var newValue = event.data.data(); 
    var artistId = newValue.artist.id; 
    var artistRef = firestore.collection('artists').doc(artistId); 

    return event.data.ref.set({ 
    artistName: artistRef.get().then(doc => { 
     if (!doc.exists) { 
     console.log('No such document!'); 
     } else { 
     console.log('Document data:', doc.data()); 
     var artistName = doc.data().name; 
     console.log('Artist Name:', artistName); 
     return Promise.resolve(artistName); 
     } 
    }) 
    }, {merge: true}); 
}); 

私は約束からデータを取得するようです。

答えて

0

まず、artistRef.get()を実行し、必要な文書を入手した後、event.data.ref.set()のデータを使用します。今書いたとおり、artistNameプロパティに約束オブジェクトを割り当てています。

パターンのこのタイプの一般的な形式は次のようになります。

// First get the artist doc 
return artistRef.get().then(doc => { 
    return event.data.ref.set({ 
     // use the properties of the doc in here 
    }) 
}) 
関連する問題