2015-11-20 37 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)}); –

相關問題