我想在Node.js中创建一个应用程序。 这个应用程序修改了一个文档,让我们说:
{_id: xxx, text: "Hello"}
每次更改此文档时,我都想在node.js应用程序中执行一个函数。 所以它大致像这样
$db.on('update', function(id){
console.log("There are changes in " + id)
})
如何使用Node.js执行此操作?
答案 0 :(得分:0)
您正在寻找couchdb的changes Feed。每次在您订阅的db
中更新文档时,都会发送通知。您只需听取该通知并执行代码即可。
您还可以订阅特定的文档ID。请参阅POST请求中的request options。
此外,由于您使用的是节点j,我建议您查看follow library。它使您想要做的事情变得更加容易。
示例: -
var follow = require('follow');
follow({db: "<database name here>", include_docs:true},
function(error, change) {
if(!error) {
console.log(change.doc) // updated doc
console.log("Got change number " + change.seq + ": " + change.id);
}
});