Need help searching for a value in firestore

后端 未结 1 731
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 10:48

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 us

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 11:18

    Link to documentation: https://firebase.google.com/docs/firestore/query-data/queries#simple_queries

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

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

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

    So how do we where it? Easy.

    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 
               });
    
    

    0 讨论(0)
提交回复
热议问题