Answer the question
In order to leave comments, you need to log in
Node.js error ERR_INVALID_CALLBACK in timers.js file how to fix?
Hello!
Using node, I convene photos on the site in webp, the script works, but with a large number of photos, the server cannot cope and the Load Average starts to grow rapidly.
I decided to set a pause between photo conversions using setTimeout(), after which the error [ERR_INVALID_CALLBACK] appeared: Callback must be a function.
Problem script:
var path = require('path');
var fs = require('fs');
var async = require('async');
var webp=require('webp-converter');
function getFiles (dirPath, callback) {
fs.readdir(dirPath, function (err, files) {
if (err) return callback(err);
var filePaths = [];
async.eachSeries(files, function (fileName, eachCallback) {
var filePath = path.join(dirPath, fileName);
fs.stat(filePath, function (err, stat) {
if (err) return eachCallback(err);
if (stat.isDirectory()) {
getFiles(filePath, function (err, subDirFiles) {
if (err) return eachCallback(err);
filePaths = filePaths.concat(subDirFiles);
eachCallback(null);
});
} else {
if (stat.isFile() && /dr5-220-[0-9]_1.jpg$/.test(filePath)) {
filePaths.push(filePath);
}
eachCallback(null);
}
});
}, function (err) {
callback(err, filePaths);
});
});
}
getFiles('/data/upload/allfoto', function (err, files) {
for(var i=0; i<files.length; i++){
function ChangePhoto(foto){
console.log(foto);
fs.access(foto + ".webp", function (error) {
if (error != null) {
webp.cwebp(error.path.slice(0, -5), error.path, "-q 70", function (status, error) {
//if conversion successful status will be '100'
//if conversion fails status will be '101'
// console.log(status,error);
});
}
});
}
setTimeout(ChangePhoto(files[i]), 300);
}
});
timers.js:390
throw new ERR_INVALID_CALLBACK();
^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at setTimeout (timers.js:390:11)
at /data/git-project/path.js:60:9
at /data/git-project/path.js:35:13
at /data/git-project/node_modules/async/dist/async.js:473:16
at replenish (/data/git-project/node_modules/async/dist/async.js:1006:25)
at iterateeCallback (/data/git-project/node_modules/async/dist/async.js:995:17)
at /data/git-project/node_modules/async/dist/async.js:969:16
at /data/git-project/path.js:31:21
at FSReqWrap.oncomplete (fs.js:154:5)
Answer the question
In order to leave comments, you need to log in
Because you don't pass a function as the first parameter to setTimeout, but the result of calling the ChangePhoto(files[i]) function.
And by the way, in this form, your photo processing will not be consecutive with a delay of 300ms between calls, and all handlers will start at once 300ms after the cycle. But due to single-threading, the nodes will still be executed in a row one after another.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question