如何在 Firestore + Ionic 4 上设置和获取一组键值?
How to set and get with an array of key values on Firestore + Ionic 4?
我正在尝试设置和获取一些 TODO 注释,但问题是我正在与所有用户共享它,并且它没有保存在我的 firestore 数据库中,不知道为什么,但它遗迹。我的模型是:
我的添加和获取方法是:
idea: Idea = {
name: '',
note: '',
date: new Date().getTime()
};
ADD:
this.afs.collection(`users/${this.user.getUID}/ideas`).add(this.idea)
GET_ALL:
this.afs.collection(`users/${this.user.getUID}/ideas`)
我的模态还好吗?我需要为每个想法设置一些 Id 吗?非常感谢
已更新:
现在我知道怎么添加了,现在只需要知道如何将我点击的数组的位置传递到详情页面来加载详情。
解决方案添加:
this.afs.doc(`users/${this.user.getUID()}`).update({
ideas: firestore.FieldValue.arrayUnion(this.idea)
})
终于解决了
这是我的代码:
# ADD #
this.afs.collection('ideas').add(this.idea)
# LOAD FROM EXISTING IDEA #
ngOnInit() {
this.ideaId = this.route.snapshot.params['id']
if (this.ideaId) {
this.loadIdea();
}
}
async loadIdea() {
this.afs.collection<Idea>(`/ideas`)
.doc<Idea>(this.ideaId)
.valueChanges()
.subscribe(data => {
console.log(data)
this.idea = data
loading.dismiss()
})
}
# REMOVE #
remove(idea: firebase.firestore.QueryDocumentSnapshot) {
firebase.firestore()
.collection('ideas')
.doc(idea.id).delete()
})
}
# GET LIST FROM IDEAS #
getIdeas(){
firebase.firestore().collection('ideas')
.where('owner', '==', this.user.getUID())
.onSnapshot(querySnapshot => {
console.log('querySnapshot: ' + querySnapshot)
this.ideas = querySnapshot.docs
})
}
我正在尝试设置和获取一些 TODO 注释,但问题是我正在与所有用户共享它,并且它没有保存在我的 firestore 数据库中,不知道为什么,但它遗迹。我的模型是:
我的添加和获取方法是:
idea: Idea = {
name: '',
note: '',
date: new Date().getTime()
};
ADD:
this.afs.collection(`users/${this.user.getUID}/ideas`).add(this.idea)
GET_ALL:
this.afs.collection(`users/${this.user.getUID}/ideas`)
我的模态还好吗?我需要为每个想法设置一些 Id 吗?非常感谢
已更新:
现在我知道怎么添加了,现在只需要知道如何将我点击的数组的位置传递到详情页面来加载详情。
解决方案添加:
this.afs.doc(`users/${this.user.getUID()}`).update({
ideas: firestore.FieldValue.arrayUnion(this.idea)
})
终于解决了
这是我的代码:
# ADD #
this.afs.collection('ideas').add(this.idea)
# LOAD FROM EXISTING IDEA #
ngOnInit() {
this.ideaId = this.route.snapshot.params['id']
if (this.ideaId) {
this.loadIdea();
}
}
async loadIdea() {
this.afs.collection<Idea>(`/ideas`)
.doc<Idea>(this.ideaId)
.valueChanges()
.subscribe(data => {
console.log(data)
this.idea = data
loading.dismiss()
})
}
# REMOVE #
remove(idea: firebase.firestore.QueryDocumentSnapshot) {
firebase.firestore()
.collection('ideas')
.doc(idea.id).delete()
})
}
# GET LIST FROM IDEAS #
getIdeas(){
firebase.firestore().collection('ideas')
.where('owner', '==', this.user.getUID())
.onSnapshot(querySnapshot => {
console.log('querySnapshot: ' + querySnapshot)
this.ideas = querySnapshot.docs
})
}