问题描述
我正在使用离子角和Firestore构建Web应用程序。当我想获取文档的特定字段时,我知道我必须映射到一个对象,这就是我所做的。
export interface notificationModel{
notificationId?: string;
message?: string;
contactId?: string;
}
export class userModel{
userId?: string;
name?: string;
notifications?: notificationModel[];
contacts?: string[];
constructor (userId,name,notifications,contacts) {
this.name = name;
this.userId = userId;
this.notifications = notifications;
this.contacts = contacts;
}
getContacts(){
return this.contacts;
}
}
这里的userinfo集合存储用户名,一个联系人列表(它是一个字符串数组)和另一个称为类型通知的通知的集合。
这是我的转换器
var userConverter = {
toFirestore: function(userModel) {
return {
userId: userModel.userId,name: userModel.name,notifications: userModel.notifications,contacts: userModel.contacts,}
},fromFirestore: function(snapshot,options){
const data = snapshot.data(options);
return new userModel(data.userId,data.name,data.notifications,data.contacts)
}
我想吸引用户并将其映射到我的用户模型/类,以便我可以自由使用数据,但似乎可以这样做。
这是我获取用户联系人的方法之一,但出现此错误
类型'AngularFirestoreDocument'中不存在属性'withConverter'
this.userInfosCollection.doc(userId)
.withConverter(userConverter)
.get().then(function(doc) {
if (doc.exists){
// Convert to City object
var user = doc.data();
// Use a City instance method
console.log(user.getContacts());
} else {
console.log("No such document!")
}}).catch(function(error) {
console.log("Error getting document:",error)
});
}
除了解决此错误外,我还想了解存储和检索这样的数据的最佳实践。谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)