【发布时间】:2017-12-20 15:01:05
【问题描述】:
我正在尝试在 Firestore 集合(“shows”)中创建一个字段(“artistName”),该字段是从另一个 Firestore 集合(“艺术家”)中的字段(“名称”)中提取的。 “shows”集合有一个引用字段(“artist”),它指向“artists”集合中的文档。要创建该字段,我使用 Google Cloud Functions。这是我的代码:
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});
});
我似乎无法从承诺中获取数据。
【问题讨论】:
标签: javascript firebase google-cloud-functions google-cloud-firestore