Will tonight be the night that I see CouchDB changes in the node.couch.js node.js library? Here's hoping.
I spend a little more time replacing node.js "promises" using the strategy that worked for me last night. I eventually get to a point that the script runs all the way through! Running it, and then making a change, I find:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://127.0.0.1:5984Nothing. Hrm... I uploaded this "changes" attribute to a design document in my
seed
database:var sys = require('sys');I took that directly from the node.couch.js README file. It ought to print out any changes. Something ain't working.
var listener = function (change) {
sys.puts(JSON.stringify(change));
}
exports.listener = listener;
I do not know how most people debug functional code, but the best I can do is "print STDERR" debugging: insert a bunch of print statement where I think the code ought to be reaching and then triggering the event as best I can. Ideally I would do this without having actually trigger changes on the CouchDB server, but I am not quite proficient enough with node.js to accomplish this quite yet.
After a bit, I track things down to the
Deligation.prototype.designDocChange
method. Highlighted are my "print STDERR" statements:Deligation.prototype.designDocChange = function (dbname, id) {When I run the code with those print-stderr statements, I find:
var d = this;
sys.puts("[" + dbname + "] " + id);
if (!this.changes[dbname]) {
sys.puts(this.baseurl+dbname);
this.changes[dbname] = new listener.Changes(this.baseurl+dbname);
this.changes[dbname].addListener("change", function(doc) {
sys.puts("[change] " + doc);
if (doc.id && doc.id.startsWith('_design')) {
d.designDocChange(dbname, doc.id);
};
})
}
sys.puts("here 1");
d.cleanup(dbname, id);
sys.puts("here 2");
// getDesignDoc(this.baseurl, dbname, id).addCallback(function(doc){
// d.handleDesignDoc(dbname, doc);
// });
getDesignDoc(this.baseurl, dbname, id, function(doc){
sys.puts("[" + dbname + "] about to handleDesignDoc: " + id);
d.handleDesignDoc(dbname, doc);
});
sys.puts("here 3");
}
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://127.0.0.1:5984After making a change... nothing. I would have expected the callback passed into
[seed] _design/meals
http://127.0.0.1:5984/seed
here 1
here 2
here 3
getDesignCode
to fire, but it is not. Checking the getDesignDoc
function, I find:var getDesignDoc = function (baseurl, dbname, id, callback) {Aw nuts. I didn't quite convert all of my
//var p = new events.Promise();
var uri = url.parse(baseurl);
var client = http.createClient(uri.port, uri.hostname)
var request = client.request('GET', '/'+dbname+'/'+id, {'accept':'application/json'});
request.addListener('response', function(response){
var buffer = '';
response.addListener("body", function(data){buffer += data});
response.addListener("complete", function(){
dbs = JSON.parse(buffer);
//p.emitSuccess(dbs);
callback(dbs);
})
})
request.close();
//return p;
}
addListener
calls to use the new event types. The "body" event is now known as "data" and the "complete" event is now "end". As is, the "body" and "complete" events never fire, ensuring that the callback function does not fire. I convert them to the new events and find... more promises.After again replacing promises and searching through the code to ensure that no more promises or "body"/"complete" events remain, and removing all of my print-stderrs, I am ready to try again. This time, when I make a change, I find:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://127.0.0.1:5984Nice!
{"seq":62,"id":"2002-08-26-grilled_chicken","changes":[{"rev":"12-b02d1379a52d16862797d71b79116dda"}]}
{"seq":62,"id":"2002-08-26-grilled_chicken","changes":[{"rev":"12-b02d1379a52d16862797d71b79116dda"}]}
I am not sure why there are two lines being printed for one change, but it is still nice to see the changes coming through anyway. I will investigate a bit more tomorrow.
Day #63
No comments:
Post a Comment