typescript - TypeScript:Firestore withConverter 和 refrences
问题描述
我对 Firestore/Firebase 相当陌生。我有这个专辑类,可以保存对父专辑的引用
export class Album {
constructor(public id: string,
public title: string,
public parent: Album | null,
) {
}
}
export class AlbumConverter implements FirestoreDataConverter<ImageData> {
toFirestore(album: Album): firebase.firestore.DocumentData {
return {
id: album.id,
title: album.title,
parent: album.parent ?.....
我的问题是,我应该如何实现toFirestore
and fromFirestore
?DataConverter
处理引用时使用的正确方法是什么?
谢谢!
解决方案
您必须将DocumentReference作为parent
.
要获得参考,您可以做例如:
function getRef = (id: string): Promise<any> {
return new Promise<Track>(async (resolve, reject) => {
const query = this.db.collection(this.collectionPath).where('id', '==', id);
const querySnapshot = await query.get();
if (querySnapshot.empty) {
return reject("not found")
}
return resolve(querySnapshot.docs[0].ref);
});
}
接着 :
const parentRef = await getRef(parentId);
const albumConverter = {
toFirestore: (album: Album) => {
return {
id: album.id,
title: album.title,
parent: parentRef
}
}
}
推荐阅读
- cassandra - Cassandra 删除/更新一行并获取其先前的值
- python - 对于在 python3 中包含范围的循环
- mqtt - 禁用 mosquitto MQTT 代理的非安全连接
- android - CSS scale() 不适用于 Android Chrome 70 和其他版本
- ruby - Rake 和 Ansible 有什么区别?
- ios - 如果我没有 nstring 直接显示它,如何在 UITextView 中显示 HTML 文件?
- python - 在 Python 中实现事件处理程序
- javascript - AJAX 调用在脚本的其余部分后完成
- python-3.x - 将特定字符串更改为多维数组中的浮点数
- python - 如何使用 ssh 在远程机器上运行 python 脚本