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:

  1. read "music" directory.
  2. for each file on directory, extract info , store in structure "jsonsong". push "jsonsong" structure called "jsonobj".
  3. 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

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -