Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created April 8, 2013 16:07
Show Gist options
  • Select an option

  • Save davetapley/5338014 to your computer and use it in GitHub Desktop.

Select an option

Save davetapley/5338014 to your computer and use it in GitHub Desktop.
exports.routes = function() {
this.get('/', function() {
var users = db.collection('users');
this.res.setHeader("Content-Type", 'text/html');
this.res.writeHead(200, {});
var that = this;
var stream = users.find().forEach(function(err, userDoc) {
if(userDoc) {
that.res.write('<h1>' + userDoc._id + '</h1>');
that.res.write(JSON.stringify(userDoc));
if(userDoc.subscriptions) {
that.res.write('<ul>');
userDoc.subscriptions.forEach(function(err, userSubscription) {
if(userSubscription) {
that.res.write('<li>' + userSubscription + '</h1>');
} else {
that.res.write('<ul>');
}
});
}
} else {
that.res.end();
return;
}
});
});
}
@davetapley
Copy link
Author

db comes from mongojs.

The exported routes are added to director like this:

  var Users = require('./users')
  router.path('/users', Users.routes);

The users collection looks like this:

>  db.users.find()
{ "_id" : ObjectId("515ba3004aa588a68ce46259") }
{ "_id" : ObjectId("515b92b3c3f53a175094eb52"), "subscriptions" : { "facebook" : { "access_token" : "test123" } }

@davetapley
Copy link
Author

We're good down to line 12, but the if on line 14 is never true.
The goal is that if a user has subscriptions, their keys (e.g. facebook) should be printed in a ul.

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