我有以下代码并收到错误:
Invalid collection reference. Collection references must have an odd number of segments
Run Code Online (Sandbox Code Playgroud)
和代码:
private void setAdapter() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("app/users/" + uid + "/notifications").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Log.d("FragmentNotifications", document.getId() + " => " + document.getData());
}
} else {
Log.w("FragmentNotifications", "Error getting notifications.", task.getException());
}
});
}
Run Code Online (Sandbox Code Playgroud) 首先,我知道 Firestore 是如何工作的,并且花了很多时间来评估不同的方法以获得良好的结构。我仍然在考虑以下情况:
有一个已知食谱的数据库。用户可以添加食谱,但必须确认它们是真实的食谱,而不仅仅是一些变体。因此,每个用户都可以从用户生成的食谱列表中选择食谱来说明他们知道如何烹饪(或添加新食谱)。
现在我希望用户与其他人分享他们的收据列表,但我不确定如何使用 Firestore 最好地实现这一点。诀窍是,我想一次显示所有食谱,而不想对它们进行分页。
我目前正在评估两种可能性:
每当用户分享他的列表时,查看该列表的用户将不得不加载整个食谱列表,这可能会导致大量的文档读取(我想实际上大约为 50,在极少数情况下可能为 1000)。
优点:
缺点:
我可以将每个已知的配方(id 和 imageURL)保存在一个数组中的用户文档(或作为单个子文档“KnownRecipes”)中。这个数组可以是
recipesKnown: [{rid: 293ndwa, imageURL: image1.com, timeAdded: 8371201332},
{rid: 9012831, imageURL: image1.com, timeAdded: 8371201871},
{rid: jd812da, imageURL: image1.com, timeAdded: 8371201118},
...
]
Run Code Online (Sandbox Code Playgroud)
优点:
缺点:
对我来说,子集合的想法似乎是解决这个问题的更“干净”的解决方案,但也许我错过了一些关于为什么这些解决方案中的一个比另一个更好的论点。
我最常见的查询如下(按重要性降序排列):
对于我的应用程序来说,能够从firebase中的集合中随机选择多个文档至关重要.
由于Firebase(我知道)没有内置本机函数来实现这样做的查询,我首先想到的是使用查询游标来选择随机的开始和结束索引,前提是我有多少文档集合.
这种方法只能以有限的方式起作用,因为每个文件都会按照其相邻文件的顺序提供; 但是,如果我能够通过其父集合中的索引选择文档,我可以实现随机文档查询,但问题是我找不到任何描述如何执行此操作的文档,即使您可以执行此操作.
这是我想要做的,考虑以下firestore架构:
root/
posts/
docA
docB
docC
docD
Run Code Online (Sandbox Code Playgroud)
然后在我的客户端(我在Swift环境中)我想编写一个可以执行此操作的查询:
db.collection("posts")[0, 1, 3] // would return: docA, docB, docD
Run Code Online (Sandbox Code Playgroud)
无论如何我能做到这一点吗?或者,有不同的方式我可以以类似的方式选择随机文档吗?
请帮忙.
database data-modeling firebase swift google-cloud-firestore
当我运行命令firebase deploy时,我收到此错误:
我部署功能
我的职责是:确保启用必要的API ...
i runtimeconfig:确保启用必要的API ...
✔timetimeconfig:启用所有必需的API
✔功能:启用所有必需的API
我的功能:准备功能目录上传...
我的功能:用于上传的打包功能(4.04 KB)
✔功能:功能文件夹上传成功
我开始发布过程(可能需要几分钟)...
我的功能:创建函数跟随者通知......
⚠功能:无法创建函数followerNotification
⚠功能:HTTP错误:400,请求有错误
⚠功能:1个功能未能部署.
功能部署有错误.要继续部署其他功能(例如>数据库),请运行:firebase deploy --except functions
错误:功能未正确部署.
遇到麻烦?尝试firebase deploy --help
其他一切都没有问题.只有当我尝试用Firebase Firestore制作东西时.
我正在尝试使用id检索我的文档,但无法弄明白.
目前我检索我的文件是这样的:
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
Run Code Online (Sandbox Code Playgroud)
我确实完整地获得了我的文档列表,但是没有文档ID.
如何为每个文档检索它?
请,我需要您的帮助才能从Firebase 版本 9获取服务器时间戳。
我尝试遵循与 Firebase v8 相同的方法:firebase.firestore.FieldValue.serverTimeStamp()但不适用于版本 9。
有没有办法在 Firebase 版本 9 中做同样的事情?
新的Firestore DB允许我存储GeoPoints.有没有办法根据它们进行查询?
因此,例如,如果我的每个集合文档都有一个location类型的字段geopoint.如何将最近的10个文档放到任意坐标?
文档似乎没有涵盖这一点.我试过这样的东西:
someRef.where('location', '>', geopoint).limit(10).get();
Run Code Online (Sandbox Code Playgroud)
显然这没有多大意义,但我只是在尝试一些东西
下面的代码工作正常,直到今天 但我现在不知道它不起作用并给出了以下错误.你能告诉我为什么吗?
错误:使用无效数据调用函数DocumentReference.set().不支持的字段值:自定义预算对象
export class Project {
id: string = null;
name: string;
budgetList?: Budget[];
}
export class Budget {
id: string;
amount: number;
contingency: number = 20;
budgetGroup: BudgetGroup = new BudgetGroup();
creationTime: string;
}
Run Code Online (Sandbox Code Playgroud)
码:
async create(data: DtoProject): Promise<Project> {
try {
const projectId: string = this.fireStore.createId();
const budgets = this.budgetProvider.createBudgets(data.budgetList, projectId);//budgets
const proj: Project = {
id: data.id,
name: data.name,
budgetList: budgets,//here it has the error
}
proj.id = projectId;
await this.fireStore.doc<Project>(`projects/${projectId}/`).set(proj));//project
}
}
createBudgets(data: Budget[], projectId: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 next.js 设置 Firebase。我在控制台中收到此错误。
\n\n\nFirebaseError:预计 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore
\n
这是我的自定义钩子之一
\nimport { onAuthStateChanged, User } from '@firebase/auth'\nimport { doc, onSnapshot, Unsubscribe } from 'firebase/firestore'\nimport { useEffect, useState } from 'react'\nimport { auth, fireStore } from './firebase'\n\nexport const useUserData = () => {\n const [username, setUsername] = useState<string | null>(null)\n\n const [currentUser, setCurrentUser] = useState<User | null>(null)\n\n useEffect(() => {\n let unsubscribe: void | Unsubscribe\n\n onAuthStateChanged(auth, (user) => {\n if (user) {\n setCurrentUser(user)\n // The …Run Code Online (Sandbox Code Playgroud) javascript firebase firebase-authentication next.js google-cloud-firestore
我有一个带有where()相等运算符的方法的查询,然后是一个orderBy()方法,但我无法弄清楚为什么它需要索引。where 方法检查对象(地图)中的值,order by 是一个数字。
文档说
如果您的过滤器具有范围比较(<、<=、>、>=),则您的第一个排序必须在同一字段上
所以我会认为平等过滤器会很好。
这是我的查询代码:
this.afs.collection('posts').ref
.where('tags.' + this.courseID,'==',true)
.orderBy("votes")
.limit(5)
.get().then(snap => {
snap.forEach(doc => {
console.log(doc.data());
});
});
Run Code Online (Sandbox Code Playgroud)
firebase ×10
javascript ×3
angular ×2
angularfire2 ×2
android ×1
database ×1
ionic3 ×1
java ×1
next.js ×1
nosql ×1
swift ×1
typescript ×1