Skip to content

Instantly share code, notes, and snippets.

@robotarmy
Created May 31, 2010 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotarmy/420252 to your computer and use it in GitHub Desktop.
Save robotarmy/420252 to your computer and use it in GitHub Desktop.
Ledger.prototype.collection = function(callback) {
var client = new DStore('experiment',new Server("127.0.0.1", 27017, {},{strict:true}));
client.open(function() {
client.createCollection('ledger', function(err, collection) {
client.collection('ledger', function(err, collection) {
callback.apply(this,[err,collection,client]);
});
});
});
};
Ledger.prototype.service = function() {
var self = this;
var http = require('http');
var sys = require('sys');
self.server = http.createServer(function(request,response) {
var req = request;
var resp = response;
self.find_all(function(err,items){
sys.puts(err);
var callback = undefined;
var query = url.parse(request.url,true).query
if (query != undefined)
callback = query['callback'];
if (callback != undefined){
var body = ";"+callback+"("+JSON.stringify(items)+");";
resp.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'application/json;charset=utf-8'
});
resp.write(body,'utf8')
resp.end();
}
});
});
self.server.addListener('close',function(errno) {
sys.puts('close ' + errno);
});
self.server.listen(7001);
};
Ledger.prototype.find_all = function(callback) {
this.collection(function(err, collection) {
collection.find(function(err, cursor) {
cursor.toArray(function(err, docs) {
callback.apply(this,[err,docs]);
});
});
});
};
# Same problems with this write of it
#
Ledger.prototype.find_all = function(callback) {
this.collection(function(err, collection, client) {
raise_if(err);
collection.find(function(err, cursor) {
raise_if(err);
docs = []
cursor.each(function(err, doc) {
raise_if(err);
docs[docs.length] = doc
});
callback.apply(this,[err,docs]);
client.close();
});
});
};
@robotarmy
Copy link
Author

thank you so much Christian for your thought and efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment