简体   繁体   English

数据添加到firestore的时候,想用onSnapshot监听,这样就可以自动调用数据了,但是不知道怎么用

[英]When data is added to the firestore, I want to listen with onSnapshot so that the data can be called automatically, but I don't know how to use

I want to listen for the value of message in the data structure in the attached image我想在附图中的数据结构中监听消息的值

I get an error with this code我收到此代码错误

secondDb.collection("chats")
  .doc(dockey)
  .onSnapshot(function (doc) {
    doc.docChanges().forEach(function (change) {
      if (change.type === "added") {
        console.log(doc.data().messages[0]);
      } else if (change.type === "modified") {
        // 
      } else if (change.type === "removed") {
        // 
      }
    })
  });

error code doc.docChanges is not a function错误代码 doc.docChanges 不是 function

The data structure is as follows:数据结构如下: 数据结构

You can access the data over the collection you want to retrieve您可以通过要检索的集合访问数据

var unsubscribe = firestore().collection('chats').onSnapshot(snapshot =>{
  snapshot .docChanges().map((change) => {
   if (change.type === "added") {
    console.log(doc.data().messages[0]);
   } else if (change.type === "modified") {
     // 
   } else if (change.type === "removed") {
     // 
   }
  })
})

Looking at your data structure, I would say that you probably don't want to call .onSnapshot on your entire collection.查看您的数据结构,我会说您可能不想在整个集合上调用.onSnapshot

Your initial code was fine.您的初始代码很好。 But, since you called .onSnapshot on a DocumentReference, you got a DocumentSnapshot .但是,由于您在 DocumentReference 上调用了.onSnapshot ,因此您得到了DocumentSnapshot Instead of .docChanges you would just do doc.data().messages .而不是.docChanges您只需执行doc.data().messages

You should also check out the documentation on Best Practices for realtime updates.您还应该查看有关最佳实践的文档以获取实时更新。

If you're trying to make a realtime messaging app, you might consider using Firebase Cloud Messaging instead of onSnapshot .如果您正在尝试制作实时消息应用程序,您可以考虑使用 Firebase Cloud Messaging 而不是onSnapshot You could load all the messages for a conversation from Firestore when a user opens the app, and receive any new ones using FCM as long as the user is connected to the internet.当用户打开应用程序时,您可以从 Firestore 加载所有对话消息,并在用户连接到互联网时使用 FCM 接收任何新消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想知道如何使用这个 JSON 数据 HackNews - i want to know how can i use this JSON data HackNews 在实时 Firestore 中返回 onSnapshot 时如何提取数据? - How to extract data when onSnapshot is returned in realtime firestore? 当我不知道文档 ID 时,如何使用 mongoose 查询子文档 - how can I use mongoose to query a subdocument when I don't know the document Id 我不知道如何正确地从 fetch 中获取数据 - I don't know how to get data from fetch correctly 为什么不听数据事件我只能发送5个请求? - why I can only send 5 request if I don't listen the data event? 我想使用DataTables列搜索。 但是我不知道服务器端代码应该如何 - I want to use DataTables column search. But I don't know how should be server side code 我想重复代码,但不知道如何使用循环 - I want to repeat code but don't know how t use loops 表单会在我不想提交时自动提交吗? - Form automatically submits when I don't want it to? 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 我需要根据 onSnapshot 调用中的 forEach 数据从另一个 Firestore 集合中提取数据 - I need to pull data from another Firestore collection based on forEach data within a onSnapshot call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM