I am facing an issue where I need to transfer data from fileExistance to result and then export the result to budget.js in the router folder. However, I am encountering the following error:
internal/validators.js:189
throw new ERR_INVALID_CALLBACK(callback);
^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received []
at setTimeout (timers.js:121:3)
at Object.<anonymous> (C:\Users\Jaemin Windows10\GitHub\Jae_tasks\controller\budget.js:18:25)
const fs = require('fs');
var result = [];
const fileExistance = () => {
fs.readdir('./data/budget', function(err, files) {
if(err){
console.log(err);
} else if(!files.length) {
return "No Transaction history";
} else {
console.log("file Existance " + files);
result.push(files);
}
});
}
fileExistance();
console.log("result " + setTimeout(result, 1000));
exports.result = result;
This is budget.js located in the router folder.
const path = require('path');
const express = require('express');
const router = express.Router();
const fs = require('fs');
var budgetController = require('./../controller/budget');
console.log("result " + budgetController.result);
router.get('/budget', (req, res, next) => {
const result = budgetController.result;
console.log(result);
res.render('budget', {
prods: result,
pageTitle: 'Budget',
path:'/budget',
hasResults: result.length > 0,
activeBudget: true,
productCSS: true
});
});
router.post('/budget', (req, res, next) => {
console.log({title: req.body.title}.title);
filePath.push({title: req.body.title}.title);
res.redirect('/budget');
});
exports.router = router;
The budget.js file in the router folder successfully retrieves data into the result. As I am not very familiar with callback functions and I am still learning JavaScript, how can I resolve this issue?