Reputation: 4302
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;
});
};
Upvotes: 1
Views: 537
Reputation: 418
The problem is, that you return "paragraph" in the callback function of waterfall. So the function sentenceObjs returns nothing, or undefined.
You need to pass in a callback function to sentenceObjs and call async.map instead of Array.map:
self.parseBodySections = function(dom, cb) {
async.map(dom, self.sentenceObjs, function(err, bodyJSON) {
console.log(bodyJSON); // prints: [ undefined, undefined, undefined, undefined, undefined ]
return cb(null, bodyJSON);
});
};
self.sentenceObjs = function(section, cb) {
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 cb(null, paragraph);
});
};
Upvotes: 1