如何使用Firestore和express.js创建onSnapshot侦听器 [英] How to create an onSnapshot listener using firestore and express.js

查看:54
本文介绍了如何使用Firestore和express.js创建onSnapshot侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过向文档添加onSnapshot侦听器来设置实时功能.当我调用api时,它可以正常工作,但是每当我修改数据时,我都会被

I am trying to set up real-time functionality by adding an onSnapshot listener to a document. It works fine when I call the api, but whenever I modify the data I get hit with a

错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

function routes() {
  const router = express.Router();

  router.route("/jobs/:company").get(async (req, res) => {
      const company = req.params.company;

      const snapshot = db
        .collection("companies")
        .doc(company)
        .collection("operations")
        .doc("jobs")

        snapshot.onSnapshot(docSnapshot=> {
          res.status(200).send(docSnapshot.data());
        }, err => {
          res.status(500).send(err)
        });

  });
}

我知道当您多次调用 res.send(something)时会发生这种错误,但实际情况并非如此.同时,我检查了Firebase节点模块,发现reference.js文件调用了一个 onNext 函数,该函数可能会触发错误.我不太确定这里发生了什么,但我会非常感谢您的帮助.

I know this sort of error occurs when you call res.send(something) multiple times, but it's not the case here. Meanwhile, I checked my firebase node module and noticed the reference.js file calling an onNext function that might be triggering the error. I am not really sure what's going on here but I would really appreciate some help.

推荐答案

onSnapshot()附加了一个持久的侦听器,该侦听器将针对文档的每次更改不断被调用.这就是为什么 send()被多次调用的原因.Express不允许这样做,因为它必须通过HTTP连接一次发送整个响应.

onSnapshot() attaches a persistent listener that will keep getting invoked for every change to the document. That's why send() is getting called more than once. Express doesn't allow this because it must send an entire response at once over the HTTP connection.

相反,您应该使用 get()一次获取数据,如文档.这里不需要持久的侦听器.

Instead, you should be using get() to fetch data a single time as illustrated in the documentation. There's no need for a persistent listener here.

      const ref = db
        .collection("companies")
        .doc(company)
        .collection("operations")
        .doc("jobs")

      ref.get()
      .then(docSnapshot=> {
        res.status(200).send(docSnapshot.data());
      })
      .catch(err => {
        res.status(500).send(err)
      });

如果您实际上是尝试通过HTTP连接向客户端发送多个文档更新,则必须考虑发送分块的HTTP编码响应,并确保您的服务器和客户端框架都可以实际使用该HTTP协议详细信息.可能会麻烦多于其应有的价值(在这种情况下,您可能应该让客户端直接使用Firestore SDK).

If you are actually trying to send multiple document updates to the client over an HTTP connection, you're going to have to look into sending a chunked HTTP encoded response, and making sure both your server and client frameworks can actually work with that HTTP protocol detail. It might be more trouble than it's worth (in which case, you should probably have the client just use the Firestore SDK directly).

这篇关于如何使用Firestore和express.js创建onSnapshot侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆