0

I've built this endpoint:

app.get("/files", async (req, res) => {
    const getFilesFromLoc = async loc => {
        let filesString = "";
        await fs.readdir(loc, async (err, files) => {
            if (err) {
              console.error("Could not list the directory.", err);
              process.exit(1);
            }

            return await files.forEach(async file => {
                const newPath = path.join(loc, file);

                return await fs.stat(newPath, async (err, stat) => {
                    if (err) {
                      console.error("Error stating file.", err);
                      return;
                    }
                    if (stat.isFile()) {
                        const extension =  path.extname(newPath);

                        if (extension === ".js" || extension === ".jsx") {
                            return await fs.readFile(newPath, "utf8", (err, string) => {
                                if (err) {
                                  console.error("Error reading file.", err);
                                  return;
                                }

                                return filesString += string;
                            });
                        }
                    } else if (stat.isDirectory()) {
                        return await getFilesFromLoc(newPath);
                    }
                })
            })
        })
        return filesString;
    }
    const filesString = await getFilesFromLoc("src/client/actions");
    res.send(filesString);
});

What it does is from a starting path, it collects every js and jsx file, converts it into a string, and pushes it to the others in fileString.

The function works well, however. the response is an empty string. Where am I doing wrong?

Gergő Horváth
  • 3,195
  • 4
  • 28
  • 64
  • `getFilesFromLoc` has no `return` statement! – Quentin Aug 08 '19 at 10:51
  • It returns fs.readdir because of the parentheses, and it updates `filesString` in the outer scope, but it looks like `res.send()` sends the initial `filesString`. I tried to pass return statement to every possibility, but doesn't work. – Gergő Horváth Aug 08 '19 at 11:06
  • Oh, you're right, it does return the result of calling readdir … but that returns `undefined` so the result is the same. – Quentin Aug 08 '19 at 11:09
  • Now I've updated the code, and it doesn't return undefined anymore, but still doesn't work. Any suggestions? – Gergő Horváth Aug 08 '19 at 11:14
  • See the duplicate. It's an even more exact match for it now. Note that you can only usefully `await` a promise and we've already established that `readdir` returns `undefined` and not a promise. – Quentin Aug 08 '19 at 11:33
  • I can't find any solution in the duplicate – Gergő Horváth Aug 08 '19 at 12:22
  • Following the suggestion to return a promise would probably be most sensible. – Quentin Aug 08 '19 at 12:33
  • Now I've used promise.all for the looping through the files, but if I start to run a function from a directory which has many files, the application hangs after a time. With less files, it finishes properly – Gergő Horváth Aug 08 '19 at 13:32

0 Answers0