在nano中处理更改Feed(跟随)的正确方法是什么?

时间:2018-04-28 13:38:16

标签: node.js couchdb couchdb-nano

我正在使用nano follow函数来监控对couchdb数据库的更改。虽然基本上工作,但我无法在不产生异常的情况下停止Feed。这是我的代码:

const nano = require("nano")("http://localhost:5984");
const name = "test";

nano.db.destroy(name, () => {
  nano.db.create(name, () => {
    const db = nano.use(name);
    const feed = db.follow({ since: "now" });

    // add doc every second
    const timer = setInterval(() => {
      db.insert({}, (err, body) =>
        console.log(err ? "ERROR" : "ADD " + body.id)
      );
    }, 1000);

    const handler = change => console.log("GOT " + change.id);
    feed.on("change", handler);

    // start feed after 5s
    setTimeout(() => feed.start(), 5000);
    // stop feed after 10s
    setTimeout(() => {
      clearInterval(timer);
      feed.stop();
    }, 10000);
  });
});

然而,这是运行时的结果:

$ node startstop.js
ADD ae1b3a77338a73da2f3cb4cf162768e2
ADD ae1b3a77338a73da2f3cb4cf162768e9
ADD ae1b3a77338a73da2f3cb4cf162769cc
ADD ae1b3a77338a73da2f3cb4cf162773b6
ADD ae1b3a77338a73da2f3cb4cf1627757a
GOT ae1b3a77338a73da2f3cb4cf1627757a
ADD ae1b3a77338a73da2f3cb4cf16277c4c
GOT ae1b3a77338a73da2f3cb4cf16277c4c
ADD ae1b3a77338a73da2f3cb4cf16277c60
GOT ae1b3a77338a73da2f3cb4cf16277c60
ADD ae1b3a77338a73da2f3cb4cf16278a7b
GOT ae1b3a77338a73da2f3cb4cf16278a7b
ADD ae1b3a77338a73da2f3cb4cf16278da7
GOT ae1b3a77338a73da2f3cb4cf16278da7
_http_client.js:355
  if (req.res && req.res.readable) {
          ^

TypeError: Cannot read property 'res' of null
    at Socket.socketCloseListener (_http_client.js:355:11)
    at Socket.emit (events.js:180:13)
    at TCP._handle.close [as _onclose] (net.js:541:12)

如何首先防止错误?或者清理我不再需要的跟踪饲料的正确方法是什么?

这些是我正在使用的软件的相关版本......

$ pacman -Q couchdb nodejs
couchdb 2.1.1-4
nodejs 9.11.1-1
$ npm list nano
test@1.0.0 /tmp/test
└── nano@6.4.3 

0 个答案:

没有答案