Answer the question
In order to leave comments, you need to log in
Node.js fs.readdir Undefined?
Good afternoon. I am developing a web application for a friend based on node.js and electron. The essence of the application is to convert the files that it takes from the folder. The user specifies the full path to the folder on the PC in the input field, then the script takes the list of files in this folder and converts them one by one, writing them to the folder created by the script. The problem is that after scrolling through a bunch of forums and both Russian-speaking and English-speaking StackOverflow, I could not solve the problem with fs.readdir().
The function outputs Undefined and I just don't know how to fix it. I decided to turn to smart people here on HabrQ&A
Here is the code:
var now = new Date();
var path = require("path");
var counter = 0;
var ffmpeg = require("ffmpeg");
const fs = require("fs");
const path2 = "Ready_Vidios";
class Video_Audio_Convertation {
constructor(f) {
this.f = f;
}
convertation(){
let newfilename = "${now.getMilliseconds()}.mp4";
try{
var process = new ffmpeg('${getnameoffolder()}\${f}');
console.log('${getnameoffolder()}\${f}');
process.then(function (video) {
video
.setAudioCodec('libfaac')
.setAudioChannels(2)
.save('${path2}\${newfilename}', function (error, file) {
if (!error)
console.log('Video file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
}catch(e){
console.log(e.code);
console.log(e.msg);
}
}
}
function convertfunc(f){
let video = new Video_Audio_Convertation();
video.convertation(f);
}
function create_new_folder() {
fs.mkdirSync(path2);
}
function getdirectories(path, callback) {
fs.readdir(path, function (err, content) {
if (err) return callback(err)
callback(null, content)
})
console.log(content);
}
function ConvertMainFunc(folder_path) {
try{
console.log("f-path in func:"+folderpath.value);
let txtlisttosend;
if(fs.existsSync(path2)){
getdirectories(folder_path.value, function(err, content){return content});
console.log(content);
txtlisttosend = "\n".join(content);
for(let f in content){
counter += 1;
convertfunc(path.join(folder_path.value, f));
}
}
if(fs.existsSync(path2) != true){
create_new_folder();
getdirectories(folder_path.value, function(err, content){return content});
console.log(content);
txtlisttosend = "\n".join(content);
for(let f in content){
counter += 1;
convertfunc(path.join(folder_path.value, f));
}
}
return txtlisttosend;
}catch(err){
}
}
function GetCounter() {
return counter;
}
function ConvertOnPress(){
let folder_path = document.getElementById("folderpath").value;
console.log(folderpath.value);
let res = ConvertMainFunc(folderpath);
document.getElementById("outputlist").innerHTML=res;
console.log(res);
let ct = GetCounter();
document.getElementById("nfiles").innerHTML="Number of converted files"+" "+ ct;
console.log(ct);
}
function animateGrid() {
anime({
targets: '.stagger-visualizer .anim-row',
scale: [
{value: .1, easing: 'easeOutSine', duration: 500},
{value: 1, easing: 'easeInOutQuad', duration: 1200}
],
delay: anime.stagger(200, {grid: [13, 1], from: 'center'})
});
}
document.querySelector(".button").onclick = function(){
ConvertOnPress();
animateGrid();
}
Answer the question
In order to leave comments, you need to log in
and should not
function ConvertMainFunc(folder_path) {
try{
console.log("f-path in func:"+folderpath.value);
let txtlisttosend;
if(fs.existsSync(path2)){
getdirectories(folder_path.value, function(err, content){return content});
// return куда?
console.log(content);
// где определена переменная content?
function getdirectories(path, callback) {
fs.readdir(path, function (err, content) {
if (err) return callback(err)
console.log(content);
callback(null, content)
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question