0

필드에서 가져 오는 Firestore 컬렉션 ("shows")에 필드 ("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 
    }) 
})