質問

In my node.js app I have the server get an atom feed from github and then I parse it using an external module. It works fine in one file (getfeed.js) but then when I export it to my app.js file and call it it returns undefined. When I log it inside of the getfeed.js script, it returns the correct data; it's just that inside of the app.js the data is undefined. Very odd. Here is the code in question:

getfeed.js

    module.exports = function() {
        var FeedParser = require("feedparser")
          , request = require("request")
          , ejs = require("ejs")
          , fs = require("fs");
        var templateString = fs.readFileSync("./views/feed.ejs", { encoding: "utf-8"});
        var req = request("https://github.com/codecademy-beginners/text-adventure/commits/master.atom")
          , feedparser = new FeedParser();
    .
    .
    .
    .
        var totalText;

        feedparser.on('error', function(error) {
        console.error("Oops. There was an error. Sorry!");
        });
        feedparser.on('readable', function() {

        var stream = this
          , meta = this.meta
          , itemData;

        totalText = "";
        while (itemData = stream.read()) {
            //console.log("TITLE: " + item.title + "\n");
            //console.log("DESCRIPTION: " + item.description + "\n");
            //console.log(item);
        totalText += ejs.render(templateString, { item : itemData } );

        }
  console.log(totalText); //This is all as expected
  return totalText;
});

};

app.js

.
.
var getFeed = require("./getfeed");
.
.
.
.
.
app.get("/feed", function(req, res) {
    var data = getFeed();
    console.log(data); //undefined
    var formattedData = "<!DOCTYPE html><html><head><title>Feed</title></head><body>" + data + "</body></html>"; //Not the prettiest thing, but it works fine
    res.send(formattedData); //Just prints undefined onto the screen :(
});
.
.
.
.
.
役に立ちましたか?

解決

feedparser is an asynchronous function. Your getfeed function returns totalText, which is undefined at that moment. You need to pass a callback to your getfeed function and invoke that callback with the result.

getfeed.js

module.exports = function(callback) {

then in your feedparser.on('readable') -

var totalText = ''
feedparser.on('readable', 
    var stream = this
      , meta = this.meta
      , itemData;

    while (itemData = stream.read()) {
        totalText += ejs.render(templateString, { item : itemData } );        
    }
});

feedparser.on('end', function() {
    callback(totalText);
});

app.js -

getFeed(function(data) { 
console.log(data); //undefined
    var formattedData = "<!DOCTYPE html><html><head><title>Feed</title></head><body>" + data + "</body></html>"; //Not the prettiest thing, but it works fine
    res.send(formattedData); //Just prints undefined onto the screen :(
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top