reactjs - Firestore 检查键是否存在,然后检查同一集合中的特定字段是否存在
问题描述
我的 Firebase firestore 是这样构建的:
duell -> documentID -> key:"l23dsk2ls",(player:uid), ...
我检查密钥是否存在,如下所示:
const db = firebase.firestore();
var duettsRef = db.collection("duetts");
export const keyExists = (setKey)=>{
duettsRef
.doc('key')
.get().then(
doc => {
if (doc.exists) {
this.db.collection('duett').doc('key').collection(setkey).get().
then(sub => {
sub.docs.length > 0
});
}
})
}
如果存在,则密钥是唯一的。现在,而不是上面我只检查密钥是否存在的代码,我想知道密钥是否存在 -> 如果是 -> 如果player:"..."
集合中有一个字段,我在其中找到了密钥。我怎样才能做到这一点?
解决方案
要检查值是否存在,您可以使用 query,它看起来像这样:
duettsRef
.where('player', '==', key)
.get().then(
snapshot => {
if (!snapshot.empty) {
推荐阅读
- javascript - $emit 在异步方法中 - Vue2
- php - Codeigniter 控制器错误警告。未定义的函数数据库项和声明兼容
- excel - 当单元格中的值更改时隐藏空行
- vim - 通过模式行激活 VIM 折叠标记
- jquery - 如何在jquery中查找元素并获取该元素之后的元素
- python - 在 tkinter 中嵌入 matplotlib 图的问题
- nasm - 在 ORG 指令后设置段寄存器
- .net - 从 .net 应用程序使用 APIM api
- data-structures - 找到不属于树的任何可能直径的边缘
- windows - 在 cli windows 命令提示符下设置当前日期和文件夹