特定文档 ID 上的事件监听器,而不是整个数据库上的事件监听器

Event Listener on particular document Id, not on whole database

如何在 couchdb 中连续监听数据库的特定文档?如果仅在该文档中有任何更改,那么并且只有在那时我才想安慰该文档,否则不会。我怎样才能做到这一点?

我在 couchdb 中的数据库条目:

{
     "_id": "my-doc",
     "_rev": "13-7cf9b1373542d93da7b484774856429d",
     "awesome": "my-doc"
}

我的代码:

var
    util = require('util'),
    couchdb = require('felix-couchdb'),
    client = couchdb.createClient(5984, 'localhost'),
    db = client.db('lookup');

db.changesStream({id:"my-doc"}, function(err,success){
    if(!err){
        console.log(success)
    }
})

此代码生成错误

    stream = new process.EventEmitter(),
             ^

TypeError: process.EventEmitter is not a constructor
    at exports.Db.Db.changesStream (/home/xyz/Projects/practice/node_modules/felix-couchdb/lib/couchdb.js:676:14)

除了我尝试使用库,例如​​:- couchdb-api、couchdb-change-events..

  1. 要监听单个文档更改,您必须提供 docs_ids=["$id"] 其中 $id 是您要跟踪的 ID。

  2. 好像没有EventEmitter。也许您正尝试在浏览器中 运行 nano?

您可以这样使用 db.changes:

const nano = require('nano')('http://localhost:5984')
const db = nano.use('foo');

const req = db.changesAsStream({
    since: "now", feed: 'continuous', doc_ids: JSON.stringify(["my-doc", "doc_2"])
}).pipe(process.stdout);

我推荐使用db.followAPI。它有更多的功能,我认为它更稳定

const nano = require('nano')('http://localhost:5984')
const db = nano.use('foo');

const feed = db.follow({since: "now",filter:"_doc_ids",doc_ids:JSON.stringify(["my-doc","doc_2"])});
feed.on('change', (change) => {
  console.log("change: ", change);
});
feed.follow();