How Do I Properly Issue Response To Post When Waiting For Async Method To Complete?
Solution 1:
Even though you solved your scope issue (which is awesome), nesting callbacks can get kind of tricky quickly (as you saw). A good way to deal with this is to use promises. The two main packages for promises are Q and Bluebird (with Bluebird being my favorite).
You can also use a package that has already promise-ified mongo calls for you, like promised-mongo
Once you have that all set up, it's just a matter of chaining .then
for successive steps, and then sending your response when the promise is resolved.
For a specific example, check out this answer and see if that helps.
Solution 2:
Ugh...
It turns out my issue was that "res" was defined both here:
exports.saveWall = function (req, res) {
...and here:
db.collection(pictureWallsCollectionName).insert(...,
function(err, res) {
...so it turns out I could call "res" all along, I was just trying to call the wrong "res" because it got redefined.
On a side note, f*** you, javascript!
Post a Comment for "How Do I Properly Issue Response To Post When Waiting For Async Method To Complete?"