我正在使用Angular和firebase创建一个应用程序。我要在该集合内创建一个称为PETS的集合的地方,文档ID当前将登录到用户UID中。在该文档中,我想根据我的表单值创建具有随机ID的文档。
-|pets
-|userId
-|RandomId which contains all the form values
-|RandomId which contains all the form values
现在,我正在使用此方法。请告诉我我需要在此处进行哪些更改。此方法创建与当前用户具有相同ID的集合。但这不是在具有用户ID的父文档中创建单独的文档。我也想用随机ID遍历那些文档,并在前面显示一个PETS列表。
addPet(ownerName, PetName, type, breed, size){
let user = this.afAuth.auth.currentUser;
if (user) {
return new Promise((resolve, reject) => {
this.afs.collection("pets").doc(user.uid).set({
OwnerName: ownerName,
Petname: PetName,
PetType: type,
PetBreed: breed,
PetSize: size
})
.then((response) => {
resolve(response)
})
.catch((error) => {
reject(error)
});
})
} else {
alert('user not logged in')
}
}
目前,您希望将用户的宠物存放在/pets/userId
。要在此位置添加每只宠物,您将需要使用一个子集合,因为文档无法容纳其他文档。以下代码添加了将存储在的宠物/pets/userId/pets/somePetId
。
addPet(ownerName, petName, type, breed, size){
let user = this.afAuth.auth.currentUser;
if (!user) {
return Promise.reject({code: 'unauthenticated', message: 'user not logged in'})
}
return this.afs.collection("pets").doc(user.uid).collection("pets").add({
OwnerName: ownerName,
OwnerID: user.uid,
PetName: petName,
PetType: type,
PetBreed: breed,
PetSize: size
});
}
但是,您还有另外两种方式可以对数据进行建模。
而不是将宠物保存在之下userId
,而是每个宠物都有自己的ID,然后将其链接到其所有者的ID。
addPet(ownerName, petName, type, breed, size){
let user = this.afAuth.auth.currentUser;
if (!user) {
return Promise.reject({code: 'unauthenticated', message: 'user not logged in'})
}
return this.afs.collection("pets").add({
OwnerName: ownerName,
OwnerID: user.uid,
PetName: petName,
PetType: type,
PetBreed: breed,
PetSize: size
});
}
要为给定用户获取一系列宠物,您可以使用以下方法:
function getPetsForCurrentUser() {
let user = this.afAuth.auth.currentUser;
if (!user) {
return Promise.reject({code: 'unauthenticated', message: 'user not logged in'})
}
return this.afs.collection("pets").where("OwnerID", '==', user.uid).get()
.then(childrenAsArrayOfObjects)
}
由于您使用的是Cloud Firestore,而不是实时数据库,因此可以将每个宠物另存为所有者的用户数据的子集合。与实时数据库不同,当您获取的数据时/users/userId
,您只会获取该特定文档的数据,而不是嵌套在该路径下的所有数据。下面的代码为每个宠物分配自己的ID,并将其保存到其所有者的子集合中:
addPet(ownerName, petName, type, breed, size){
let user = this.afAuth.auth.currentUser;
if (!user) {
return Promise.reject({code: 'unauthenticated', message: 'user not logged in'})
}
return this.afs.collection("users").doc(user.uid).collection("pets").add({
OwnerName: ownerName,
OwnerID: user.uid,
PetName: petName,
PetType: type,
PetBreed: breed,
PetSize: size
});
}
要为给定用户获取一系列宠物,您可以使用以下方法:
function getPetsForCurrentUser() {
let user = this.afAuth.auth.currentUser;
if (!user) {
return Promise.reject({code: 'unauthenticated', message: 'user not logged in'})
}
return this.afs.collection("users").doc(user.uid).collection("pets").get()
.then(childrenAsArrayOfObjects)
}
使用此数据结构,如果要查询所有宠物,无论它们的所有者如何,都可以使用集合组查询。
let ragdollCatsQuery = db.collectionGroup('pets').where('PetType', '==', 'Cat').where('PetBreed', '==', 'Ragdoll');
ragdollCatsQuery.get()
.then(querySnapshot => {
console.log("Found " + querySnapshot.size + " cats with the breed: Ragdoll");
})
请使用一致的机壳样式,以防止错误和错别字。例如,更改Petname
以PetName
匹配数据库密钥OwnerName
的TitleCase。同样,约定是将camelCase用作变量名,因此变量PetName
应为petName
。
上面的函数都使用以下转换函数:
function childrenAsArrayOfObjects(querySnapshot, idFieldName) {
let idFieldName = (idFieldName && idFieldName + "") || "id";
let children = [];
querySnapshot.forEach(childDoc => {
children.push({...childDoc.data(), [idFieldName]: childDoc.id})
}
return children;
}
如果要在使用此转换时为文档ID使用其他字段名,则可以使用:
.then(querySnapshot => childrenAsArrayOfObjects(querySnapshot, "PetID"))
您可以在此处找到其他转换。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句