【问题标题】:Fetch data realtime from Firestore with where condition使用 where 条件从 Firestore 实时获取数据
【发布时间】:2020-04-01 08:16:43
【问题描述】:

我有一个使用whereget 从Firestore 获取数据的函数。问题是如果添加新记录,它不会实时推送到客户端。我知道我必须使用.valueChanges(),但我还需要使用.where() 条件获取数据,因为我无法获取整个数据

我只需要获取logged in user 的数据。这就是为什么需要where 条件的原因。

如何同时使用两者?

这是我当前的代码:

assign_by(){
  this.goalList =[];
  this.storage.get('loggedInUser').then((val) => {
    console.log('Your user ID is', val);
    this.loggedInusr = val;

    firebase.firestore().collection(`todos`)
    //.where("assignTo", "==", this.loggedInusr)
   .where("assignBy", "==", this.loggedInusr)
   //.where("followers", "array-contains", this.loggedInusr)
    .get()
   //.valueChanges()
    .then(querySnapshot => {
        querySnapshot.forEach(doc=> {
            // doc.data() is never undefined for query doc snapshots
            console.log("assignby");
            console.log(doc.id, " ===> ", doc.data());
            this.goalList.push(doc.data());
        });
      });

  });
}

编辑 1

按照指导,我已将当前代码更改如下:

assign_to(){

      this.goalList =[];
      this.storage.get('loggedInUser').then((val) => {
        console.log('Your user ID is', val);
        this.loggedInusr = val;
        firebase.firestore().collection(`todos`)
        .where("assignTo", "==", this.loggedInusr)
       .onSnapshot(function(querySnapshot) {           
        querySnapshot.forEach(function(doc) {
            console.log(doc.data());
            console.log("assignby");
            console.log(doc.id, " ===> ", doc.data());
            this.goalList.push(doc.data());
        });
    });
      });
    }

新记录由登录用户添加,因此新记录应显示在此屏幕上,显示该用户分配的所有记录。

我添加了这条记录:

但是这里没有更新:

编辑 2

如上图所示的assign_by()函数来自这个函数。请注意,如果我再次打开此页面,可以看到新记录。

assign_by(){
  this.goalList =[];
  this.storage.get('loggedInUser').then((val) => {
    console.log('Your user ID is', val);
    this.loggedInusr = val;

    firebase.firestore().collection(`todos`)
    //.where("assignTo", "==", this.loggedInusr)
   .where("assignBy", "==", this.loggedInusr)

    // .get()
    // .then(querySnapshot => {
      .onSnapshot(function(querySnapshot) {           
        querySnapshot.forEach(function(doc) {
            console.log(doc.data());
            console.log("assignby");
            console.log(doc.id, " ===> ", doc.data());
            this.goalList.push(doc.data());
        });
    });

        // querySnapshot.forEach(doc=> {
        //     // doc.data() is never undefined for query doc snapshots
        //     console.log("assignby");
        //     console.log(doc.id, " ===> ", doc.data());
        //     this.goalList.push(doc.data());
        // });
      // });

  });
}

【问题讨论】:

  • 提醒:人称代词为大写字母“I”,无一例外。如果您难以记住这一点,请在浏览器中安装英文拼写检查器,并确保在发布前使用它。
  • 人称代词是大写字母“I”....我已经注意到这一点:)
  • 提醒:人称代词为大写字母“I”,无一例外。如果您难以记住这一点,请在浏览器中安装英文拼写检查器,并确保在发布前使用它。

标签: angular typescript firebase google-cloud-firestore


【解决方案1】:

您可以很好地在Query 上调用onSnapshot() 方法,如下所示:

    firebase.firestore().collection('todos')
   .where("assignBy", "==", this.loggedInusr)
    .onSnapshot(function(querySnapshot) {           
        querySnapshot.forEach(function(doc) {
            console.log(doc.data());
        });
    });

通过firebase.firestore().collection('todos').where("assignBy", "==", this.loggedInusr).get(),您实际上只查询了一次数据库,因此在此之后您无法检测到集合(对应于您的查询)中是否有任何更改(例如添加的记录)初始和唯一获取。请参阅get() 方法文档here

【讨论】:

  • 我已经按照你的指导做了,我已经更新了Edit:: 1下的问题,但仍然没有更新,我还是做错了吗?
  • 从添加到您问题中的屏幕截图看来,this.loggedInusr(即Ddy1...)对应于assignBy 而不是assignTo,但您的查询是.where("assignTo", "==", this.loggedInusr)
  • 截图是My assigned works,它使用函数assign_by,我也用这个函数更新了问题,你可以看到Edit::2
  • 对不起,我不明白你的 cmets 和更新。你还有问题吗?如果是,请通过分享确切的元素来非常准确地解释它。
  • 这个解决方案对我不起作用。不存在“onSnapshot”功能。 FirebaseFirestore.instance .collection('users') .where('userId', isEqualTo: loggedInUserId) .onSnapshot(function(querySnapshot) { querySnapshot.forEach(function(doc) { console.log(doc.data()); } ); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多