我正在尝试在Firestore Collection(" show")中创建一个字段(" ArtistName"),该字段是从另一个Firestore Collection中从字段(" name")中拉出的("艺术家)")。"显示"集合具有一个参考字段("艺术家"),该字段指向"艺术家"集合中的A文档。要创建字段,请使用Google Cloud功能。这是我的代码:
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});
});
我似乎无法从承诺中获取数据。
您需要先进行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
})
})