ファイルを読み込んで適切に解析していますが、出力の文字列を返すことができないようです。クライアントで割り当てられる変数からこの文字列にアクセスできるようにしたいと思います。私は非同期シリーズを使用して、コールバック地獄を緩和し、出力がコンソールに正常にヒットします。ただし、リターン出力を同じ場所にドロップすると、機能しません。提案?
embed_analytics: function(){
var output;
async.series({
read_file: function(callback){
fs.readFile(__rootpath+'/apps/analytics/data/analytics.json', 'UTF-8', function(err,data){
if(err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
try {
var config = JSON.parse(data);
callback(null, config);
}
catch(exception) {
console.error("There was an error parsing the json config file: ", exception);
process.exit(1);
}
});
}
},
function(err, results) {
_.each(results.read_file, function(element){
output+="$('"+element.Selector+"').click(function(){_gaq.push(['_trackEvent',"+element.Category+","+element.Action+","+element.Label+"]);});\n";
});
console.log(output);
}
);
}