为什么值不会插入到数组中?

时间:2018-03-07 19:43:24

标签: javascript node.js asynchronous foreach

我有一个NodeJs应用程序,我正在尝试读取mp3文件的ID3标签。在这种情况下,第一个foreach循环工作,我可以将数组打印到屏幕。 第二个foreach循环在从循环内部将其记录到控制台时起作用,但值不会被推送到数组。该数组只返回空。

任何帮助将不胜感激。谢谢

app.get('/readfiles', function(req, res){

var fileName = [];
var tags = [];


const testFolder = './tmp/';

fs.readdir(testFolder, (err, files) => {
    files.forEach(file => {
        fileName.push(file);
    });

    files.forEach(file => {
        new jsmediatags.Reader('./tmp/'+ file)
        .setTagsToRead(["title", "artist"])
        .read({
            onSuccess: function(tag) {
            tags.push(tag.tags.title);
        },
            onError: function(error) {
            console.log(':(', error.type, error.info);
        }
        });
    });

    res.render('display', {
        file: fileName,
        tags: tags
    });
});

});

0 个答案:

没有答案