3 ms·
Wrapping things in objects doesn't seem to help me. I'd still end up with this, thanks to callbacks. app.get('/documents', function(req, res) { Docu
by bphogan 15y ago
Wrapping things in objects doesn't seem to help me.
I'd still end up with this, thanks to callbacks.
app.get('/documents', function(req, res) {
Document.find().all(function(documents) {
// 'documents' will contain all of the documents returned by the query
res.send(documents.map(function(d) {
// Return a useful representation of the object that res.send() can send as JSON
return d.__doc;
}));
});
});
Seems to me that what's missing is something that lets me write it in a more simple way. What I have here looks like an absolute nightmare of coupling. So if you have some suggestions you could point me to that show a little more detail, I'd be really grateful.
- MatthewPhillips 15y agoI would create a DocumentRequest object that takes your req and res, then break apart your callbacks to separate functions on the DocumentRequest prototype. Then you would simply consume it with: app.get('/documents', new DocumentRequest);
- bphogan 15y agoThanks for this. Been thinking about this. Could you recommend a book on patterns like these?