【问题标题】:Firestore query has somehow grouped the same users togetherFirestore 查询以某种方式将相同的用户分组在一起
【发布时间】:2020-10-15 15:53:53
【问题描述】:

我在这里有这个 Firestore 代码,我想通过 createdAt desc 来订购它。但是,当我运行它时,它还将所有相同的用户分组在一起。您可以在https://gamecrawl.com 看到相同的用户放在一起的效果。我在这里做错了什么。

只有在我添加了一个查询以从 users 集合中获取昵称后才会发生这种情况。

var self = this;
let db = firebase.firestore();
db.collection("posts")
  .orderBy("createdAt", "desc")
  .get()
  .then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      self.channelId = doc.data().ytChannelId;
      self.userId = doc.data().uid;

      let getUser = db.collection("users").where("uid", "==", self.userId);
      getUser.get().then(function(snapshot) {
        snapshot.forEach(function(snapDoc) {
          self.nickname = snapDoc.data().username;
          console.log("nickname 73493");
          console.log(self.nickname);

          self.queryData.push({
            index: self.currentIndex,
            pid: doc.data().pid,
            title: doc.data().ytTitle,
            description: doc.data().ytDescription,
            imageurl: doc.data().ytImageUrl,
            ytid: doc.data().ytid,
            channelTitle: doc.data().ytChannelTitle,
            channelId: doc.data().ytChannelId,
            recommendation: doc.data().recommendation,
            userphoto: doc.data().photourl,
            votes: doc.data().noOfVotes,
            nickname: self.nickname
          });
          self.currentIndex = self.currentIndex + 1;
        });
      });

      // console.log(doc.id, " => ", doc.data());
    });
  })
  .catch(function(error) {
    console.log("Error getting documents: ", error);
  });
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    我只是使用计算属性对 queryData 进行排序,并且它以某种方式工作。

    computed: {
    sortedQueryData: function() {
      this.queryData.sort(function(a, b) {
        var keyA = new Date(a.createdAt),
          keyB = new Date(b.createdAt);
        // Compare the 2 dates
        if (keyA > keyB) return -1;
        if (keyA < keyB) return 1;
        return 0;
      });
      console.log(self.queryData);
      return this.queryData;
    }
    },
    

    我不确定为什么将它组合在一起,我仍在寻找更好的方法来做到这一点。不知何故,这个临时解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多