需要帮助在Firestore中搜索值 [英] Need help searching for a value in firestore

查看:27
本文介绍了需要帮助在Firestore中搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firestore的新手,正在制作vue的注册页面.在创建新用户之前,它必须检查给定的用户名是否已经存在,如果不存在,请创建一个新用户.

I'm new to firestore and I'm making a register page with vue. Before a new user is made, it has to check if the given username already exists or not and if not, make a new user.

我可以向数据库中添加一个新用户,但是我不知道如何检查用户名是否已经存在.我尝试了很多事情,这是我得到的最接近的东西:

I can add a new user to the database, but I don't know how to check if the username already exists or not. I tried a lot of things and this is the closest I've gotten:

db.collection("Users")
           .get()
           .then(querySnapshot => {
             querySnapshot.forEach(doc => {
               if (this.username === doc.data().username) {
                 usernameExist = true;                              
               }
             });
           });

有人有什么主意吗?

推荐答案

文档链接: https://firebase.google.com/docs/firestore/query-data/queries#simple_queries

您可以在此查询中 where ,这对您有多种好处:

You can where this query, which is beneficial to you in multiple ways:

1:撤回的文档更少=更少的读取=降低您的成本.

1: Fewer docs pulled back = fewer reads = lower cost to you.

2:客户端工作量减少=更好的性能.

2: Less work on the client side = better performance.

那我们怎么在哪里呢?容易.

db.collection("Users")
           .where("username", "==", this.username)
           .get()
           .then(querySnapshot => {
             //Change suggested by Frank van Puffelen (https://stackoverflow.com/users/209103/frank-van-puffelen)
             //querySnapshot.forEach(doc => {
             //  if (this.username === doc.data().username) {
             //    usernameExist = true;                              
             //  }
             //});
             usernameExists = !querySnapshot.empty 
           });

这篇关于需要帮助在Firestore中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆