2015-11-20 38 views
0

我想有getCurrentSongData函数返回从刮刀传入的songdata对象,但我不断收到这样的输出:无法从另一个js文件中的NodeJS出口对象

******************TESTING**************** 
c:\Users\(PATH TO PROJECT FOLDER)\node_modules\mysql\lib\protocol\Parser.js:82 
     throw err; 
     ^

TypeError: Cannot read property 'artist' of undefined 
    at getCurrentSongData 
... 

这里的我的routes.js文件的一部分,需要我的刮板。

function getCurrentSongData(un) { 

    var scraper = require('./scrape'); 

    console.log("******************TESTING****************"); 
    console.log("testfunct: " + scraper(un).artist); 

     return scraper(un); 

} 

Here's the scraper

回答

0

你让在scrape.js异步要求,所以你需要使用返回获取歌曲数据的回调,否则,你得到undefined因为module.exports功能不返回任何东西。

function getCurrentSongData(un) { 
    var scraper = require('./scrape'); 
    scraper(un, dataLoaded); 
} 

function dataLoaded(songData) { 
    console.log(songData.artist); 
} 
+0

我不知道如何将其应用于我的代码。函数dataLoaded是否在我的routes.js文件中或在scraper中? –

+0

@NickR。 http://pastebin.com/cKNL9dij –

+0

所以如果我试图从scraper的结果加载到routes.js文件我会把这个在routes.js?它似乎没有工作。 我也有一条路线: res.render('nowplaying.ejs',{songdata:getCurrentSongData(lastfm)}); –

相关问题