Created
June 3, 2013 20:37
-
-
Save papajuans/5701193 to your computer and use it in GitHub Desktop.
Bulk get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Expecting something like /photos/bulk?ids=123,456 | |
app.get('/photos/bulk', function(req, res, next) { | |
var ids = req.query.ids.split(','); | |
Photo.find({_id : {$in: ids}}, function(err, photos) { | |
if(err) { | |
next(err) | |
} else if (photos) { | |
var wrapped = {}; | |
for(var i = 0; i < photos.length; i++) { | |
var p = photos[i]; | |
wrapped[p._id.toString()] = p; | |
} | |
res.send(wrapped); | |
} | |
}); | |
}); | |
/* Shitty test */ | |
it('should respond with multiple documents', function (done) { | |
supertest(app) | |
//.get('/photos/bulk?ids=' + ids.join(',')) | |
.get('/photos/bulk') | |
.query('ids='+ids.join(',')) | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200, function(err, res) { | |
var photos = res.body; | |
console.log(util.inspect(res.body)); | |
should.exist(photos[ids[0]]); | |
should.exist(photos[ids[1]]); | |
done(); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment