我想使用這個腳本: https://github.com/peol/node-spotifyJavascript函數VAR全球
我無法弄清楚如何使
spotify.search({ type: 'track', query: 'dancing in the moonlight' }, function(err, data){
return data;
});
數據globaly可用?
我試圖像戰利品var data =
前面那個,但它並沒有幫助
我想使用這個腳本: https://github.com/peol/node-spotifyJavascript函數VAR全球
我無法弄清楚如何使
spotify.search({ type: 'track', query: 'dancing in the moonlight' }, function(err, data){
return data;
});
數據globaly可用?
我試圖像戰利品var data =
前面那個,但它並沒有幫助
如果你需要數據可用「全球」在當前文件的範圍,只是定義了呼叫之外的另一個變種:
var myData;
spotify.search({ type: 'track', query: 'dancing in the moonlight' }, function(err, data){
myData = data;
});
如果你需要它是全球可用在您的節點的過程,然後執行以下操作:
spotify.search({ type: 'track', query: 'dancing in the moonlight' }, function(err, data){
global.myData = data;
});
是異步調用? –
您應該查看http://stackoverflow.com/q/14220321/218196以瞭解異步代碼的工作原理。雖然您可以將價值全球化,但您在訪問時必須注意*。正如答案中所證明的,將回復傳遞給回調是一種更好的方法。或者使用promise,也有它們的Node庫。 –