Sunday, April 4, 2010

Node.Couch.js on Node.js 0.1.33

‹prev | My Chain | next›

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

Nothing. Hrm... I uploaded this "changes" attribute to a design document in my seed database:
var sys = require('sys');
var listener = function (change) {
exports.listener = listener;
I took that directly from the node.couch.js README file. It ought to print out any changes. Something ain't working.

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) {
var d = this;
sys.puts("[" + dbname + "] " + id);
if (!this.changes[dbname]) {
this.changes[dbname] = new listener.Changes(this.baseurl+dbname);
this.changes[dbname].addListener("change", function(doc) {
sys.puts("[change] " + doc);
if ( &&'_design')) {

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");
When I run the code with those print-stderr statements, I find:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js
[seed] _design/meals
here 1
here 2
here 3

After making a change... nothing. I would have expected the callback passed into getDesignCode to fire, but it is not. Checking the getDesignDoc function, I find:
var getDesignDoc = function (baseurl, dbname, id, callback) {
//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);
//return p;
Aw nuts. I didn't quite convert all of my 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

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