send variables to jade template engine in nodeJs ( synchronize )

send variables to jade template engine in nodeJs ( synchronize )

Problem

I want to call a function in Node.js, get the results, and send them to a template. results always returns empty.

What’s wrong?

var replace = req.params.replace || "";
var find = req.params.find;
var fs = require("fs");
var dir = "./public/sounds/";
var results = [];

var readFiles = function (root) {
    fs.readdir(root, function (err, files) {
        if (err) {
            console.log(err);
            return;
        }
        files.forEach(function (file) {
            console.log(root.concat(file));
            fs.stat(root.concat(file), function (err, stat) {
                if (stat && stat.isDirectory()) {
                    readFiles(root.concat(file + "/"));
                }
            });
            if (file.indexOf(find) > 0) {
                var oldPath = root.concat(file);
                var newPath = oldPath.replace(find, replace).trim();
                console.log("Old Path: ", oldPath, " New Path: ", newPath);
                fs.rename(oldPath, newPath);
                results.push(newPath);
            }
        })

    });
};
readFiles(dir);
res.jsonp({
    message: results
});
Problem courtesy of: Navid_gh

Solution

var results = [];
...some async stuff...
console.log(results);

Will print the empty array every time, so how to fix.

Short answer:

var async = require('async');
async.each(files, function (file) { ... },
  function (error, results) { 
    res.jsonp({
      message: results
    });
  });

longer answer, your code branches awkwardly so it needs some reorganization to work right.

var async = require('async');
var replace = req.params.replace || "";
var find = req.params.find;
var fs = require("fs");
var dir = "./public/sounds/";


var readFiles = function (root, cb) {
    var results = [];
    fs.readdir(root, function (err, files) {
        if (err) {
            cb(err);
            return;
        }
        aync.each(file, function (file, cb) {
            console.log(root.concat(file));
            fs.stat(root.concat(file), function (err, stat) {
                if (err) {
                  return cb(err);
                }
                if (stat && stat.isDirectory()) {
                    readFiles(root.concat(file + "/"), function (err, res) {
                      results = results.concat(res);
                      cb(err);
                    });
                } else {
                    if (file.indexOf(find) > 0) {
                        var oldPath = root.concat(file);
                        var newPath = oldPath.replace(find, replace).trim();
                        console.log("Old Path: ", oldPath, " New Path: ", newPath);
                        fs.rename(oldPath, newPath);
                        results.push(newPath);
                    }
                    cb();
                }
            });

        }, function (err) {
            cb(err, results);
        });

    });
};
readFiles(dir, function (err, results) {
  res.jsonp({
    message: results
  });
});

I haven’t tested it so let me know if you have trouble debugging it.

Solution courtesy of: generalhenry

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *