I ended last night's exploration of node.couch.js / node.js with this backtrace when attempting to query my CouchDB server for a list of DBs:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://localhost:5984To resolve this, I change the
Error: finish() has been renamed to close() and no longer takes a response handler as an argument. Manually add a 'response' listener to the request object.
at ClientRequest.finish (http:317:9)
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:33:11
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:179:37
at Object.<anonymous> (/home/cstrom/repos/node.couch.js/changes/lib/service.js:190:3)
at Module._compile (node.js:721:23)
at node.js:749:20
at fs:51:23
at node.js:810:9
allDbs
function on line 33:var alldbs = function (port, hostname, pathname) {Rather than
//var p = new events.Promise();
var p = {};
var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs', {'accept':'application/json'});
request.finish(function(response){
var buffer = '';
response.addListener("body", function(data){buffer += data});
response.addListener("complete", function(){
dbs = JSON.parse(buffer);
p['addCallback'] = function (callback) {
// call the callback
}
//p.emitSuccess(dbs);
})
})
return p
}
finish
ing the request, it now has to be close
d and listeners added:var alldbs = function (port, hostname, pathname) {With that, I get:
//var p = new events.Promise();
var p = {};
var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs', {'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['addCallback'] = function (callback) {
// call the callback
};
//p.emitSuccess(dbs);
});
});
request.close();
return p;
};
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://localhost:5984Progress!
TypeError: Object #<an Object> has no method 'addCallback'
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:181:58
at Object.<anonymous> (/home/cstrom/repos/node.couch.js/changes/lib/service.js:192:3)
at Module._compile (node.js:721:23)
at node.js:749:20
at fs:51:23
at node.js:810:9
Not quite where I'd like to be, but a step forward nonetheless. I am somewhat surprised that I am getting an error here, I do define the
// ...I have changed two things in here, the
p['addCallback'] = function (callback) {
// call the callback
};
// ...
addListener('response')
and replacing the "promise" thingy from the other night. I spend the bulk of my night investigating the former. I eventually try the addListener in a standalone node.js script to see that it actually fires something off (which it does). But it will not work in the node.couch.js script...Until I define the addCallback in the psuedo "promise" that I replaced the deprecated promise with:
var alldbs = function (port, hostname, pathname) {Tomorrow I need to figure out what "promises" in node.js used to be and how to replace them in this specific case. Ultimately I need whatever function is supplied to
//var p = new events.Promise();
var p = {
addCallback: function(foo) {
sys.puts('foo');
}
};
var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs');
request.addListener('response', function(response) {
//...
addCallback()
to be called with the list of DBs on the CouchDB server. The code that is in there get the list of the DBs, but not until after the callback has already been invoked. This, I think is what the "promise" used to do. Now I just need to get it working in a post-promise world.Day #62
Hope this helps, a quote from the nodejs google group:
ReplyDeleteIf you need promises for legacy code, then include this file in your
application: http://gist.github.com/310562.
@scott Oooh! That does help, thanks :)
ReplyDeleteI think I'll give it a shot without promises first, but it is good to know that there is a fallback.