简体   繁体   中英

How to get all documents but one from firebase firestore in react native?

Since I can't use != , I''m using < and > but I don't know how to merge them together and await the result. It works for only one at a time.

  async getAllUsersExceptCurrent() {
    const lower = firebase
      .firestore()
      .collection('users')
      .where(
        firebase.firestore.FieldPath.documentId(),
        '<',
        firebase.auth().currentUser.uid
      )
      .get();

    const upper = firebase
      .firestore()
      .collection('users')
      .where(
        firebase.firestore.FieldPath.documentId(),
        '>',
        firebase.auth().currentUser.uid
      )
      .get();

    const usersList = await lower.then(snapshot => {
      return snapshot.docs.map(user => {
        console.log(user.data());
        return { id: user.id, username: user.data().username };
      });
    });

    return usersList;
  }

The following should do the trick:

    async getAllUsersExceptCurrent() {
            const lower = firebase
                .firestore()
                .collection('users')
                .where(
                    firebase.firestore.FieldPath.documentId(),
                    '<',
                    firebase.auth().currentUser.uid
                )
                .get();

            const upper = firebase
                .firestore()
                .collection('users')
                .where(
                    firebase.firestore.FieldPath.documentId(),
                    '>',
                    firebase.auth().currentUser.uid
                )
                .get();

            const [l, u] = await Promise.all([lower, upper]);

            const lArray = l.docs.map(user => {
                return { id: user.id, username: user.data().username };
            });

            const uArray = u.docs.map(user => {
                return { id: user.id, username: user.data().username };
            });

            return lArray.concat(uArray);
        }

The following would also work:

    //...
    const results = await Promise.all([lower, upper, lower]);

    const usersList = [];
    results.forEach(r => {
      r.docs.map(user => {
        usersList.push({ id: user.id, username: user.data().username });
      });
    });
    return usersList;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM