javascript - Node JS: storing music metadata on JSON -
i'm begginer in node js , i'm trouble trying build json object.
i've found module find metadata of mp3 file , want store information on json obj.
the behaviour of applicaiton following:
- read "music" directory.
- for each file on directory, extract info , store in structure "jsonsong". push "jsonsong" structure called "jsonobj".
- make console.log of "jsonobj" see result.
here's code:
var fs = require('fs'); var mm = require('musicmetadata'); fs.readdir('./music/', function(err,files) { if(err) throw err; var jsonobj = { songs: [] }; files.foreach(function(file){ var jsonsong = { title:"", artist:"", duration:"", pic:"" } var parser = new mm(fs.createreadstream('./music/'+file)); parser.on('title', function(result) { jsonsong.title = result; }); parser.on('albumartist', function(result) { jsonsong.artist = result; }); parser.on('duration', function(result) { jsonsong.duration = result; }); parser.on('picture', function(result) { jsonsong.pic = result; }); jsonobj.songs.push(jsonsong); }); console.log(jsonobj); });
my problem here "jsonsong" seems empty, here's result of last console.log (i have 3 songs on music dir, have 3 entrys on structure):
{ songs: [ { title: '', artist: '', duration: '', pic: '' }, { title: '', artist: '', duration: '', pic: '' }, { title: '', artist: '', duration: '', pic: '' } ] }
thanks time.
you have wait callbacks execute before data gets populated, can use done event.
parser.on('done', function (err) { console.log(jsonobj); });
Comments
Post a Comment