Skip to content

Instantly share code, notes, and snippets.

@sivy
Created October 19, 2010 18:20
Show Gist options
  • Save sivy/634725 to your computer and use it in GitHub Desktop.
Save sivy/634725 to your computer and use it in GitHub Desktop.
issue with collection.update callback params
/**
update stats for the URL
*/
hit_emitter.on('newHit', function(url, hit){
var data = { "$push": { "stats.hits": hit }, "$inc": { "stats.hitcounter": 1 } }
urlProvider.update(url, data, function(error, url){
console.log('updated hit: ' + JSON.stringify(url));
});
});
null
{ '$push':
{ 'stats.hits':
{ created_on: Tue, 19 Oct 2010 18:12:58 GMT
, referer: 'http://localhost:3000/'
}
}
, '$inc': { 'stats.hitcounter': 1 }
, modified_on: Tue, 19 Oct 2010 18:12:58 GMT
}
updated hit: {"$push":{"stats.hits":{"created_on":"2010-10-19T18:12:58.684Z","referer":"http://localhost:3000/"}},"$inc":{"stats.hitcounter":1},"modified_on":"2010-10-19T18:12:58.686Z"}
UrlProvider.prototype.update = function(url, data, callback) {
this.getCollection(function(error, urls_collection) {
if( error ) {
callback(error);
} else {
data.modified_on = new Date();
console.log('updating url with ' + sys.inspect(data));
urls_collection.update(
{ _id:url._id },
data,
function(error, url) {
console.log(sys.inspect(error));
console.log(sys.inspect(url)); // url is actually == data
if (error) {
console.log(error.message)
callback(error)
} else {
callback(null, url);
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment