Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two functions below (they are taken out of a larger script so assume everything is defined etc. self.sentenceObjs works great. It returns an object exactly like it's supposed to do. self.parseBodySections for some reason sets bodyJSON to an array of undefined even though self.sentenceObjs is returning perfect objects given the dom array of objects I want mapped. For some reason when I run dom.map(self.sentenceObjs) it returns undefined for each object. Any idea why this would be? Is there something with Array.map() that I missing?

  self.parseBodySections = function(dom, cb) {
    var bodyJSON = dom.map(self.sentenceObjs);
    console.log(bodyJSON); // prints: [ undefined, undefined, undefined, undefined, undefined ]

    return cb(null, bodyJSON);
  };

  self.sentenceObjs = function(section) {
    var paragraphToTextAndLinks = function(cb) {
      return self.paragraphToTextAndLinks(section.children, function(err, paragraphText, links) {
        if (err) {
          return cb(err);
        }

        return cb(null, paragraphText, links);
      });
    };

    return async.waterfall([
      paragraphToTextAndLinks,
      self.paragraphToSentences
    ],
    function(err, sentences, paragraphPlaintext) {
      var paragraph = {
        type: section.name,
        value: paragraphPlaintext,
        children: sentences
      };

      console.log(paragraph) // prints perfect object (too long to show here)

      return paragraph;
    });
  };
share|improve this question
1  
What does waterfall return? I'm not very familiar with the library but by reading at the docs it seems like it doesn't actually return the compound of the functions, it exposes it into a callback. Can you check console.log(async.waterfall([... and see what it outputs? – elclanrs 21 hours ago
Please post a short, self-contained example that demonstrates the problem. – ruakh 21 hours ago
@elclanrs Yeah I think it's something with that because it returns undefined. I'm pretty new to the library myself so I'm probably misunderstanding something. – chromedude 21 hours ago
I think you just need to run parseBodySections inside the callback of waterfall, not the other way around, since waterfall is the asynchronous part. I may be wrong tho... – elclanrs 21 hours ago
@elclanrs yeah that sounds like it might solve the issue. Thanks for trying! – chromedude 21 hours ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.