What is the most effective way for me to utilize callback functions and setTimeout in my code?

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?

Answer №1

budget.js located in the controller folder

const fs = require('fs');

let fileStatus = new Promise(function(resolve, reject) {
    fs.readdir('./data/budget', function(err, files) {
        if(err){
            console.log(err);
        } else if(!files.length) {
            resolve("No transaction history found");
        } else { 
            console.log("File exists: " + files);
           resolve(files);
        }
    });
});


exports.fileStatus = fileStatus;

budget.js stored in the router folder

router.get('/budget', (req, res, next) => {
      budgetController.fileStatus.then(function(data){
      console.log(data[0])
      res.render('budget', {
      prods: data,
      pageTitle: 'Budget',
      path:'/budget',
      hasData: data.length > 0,
      activeBudget: true,
      productCSS: true
    });
  });
});

Answer №2

Your code currently faces a race condition issue. The function fs.readdir is asynchronous, meaning it takes some time to complete its task. By calling this function, you are essentially telling it, "Go ahead and do your thing, but let me know when you're done." However, there's no guarantee that the results will be available by the time you need them. If the route is accessed swiftly, the result may end up empty.

To address this problem, consider transforming fileExistance into a callback-based function that only triggers once the results are ready. One way to achieve this is by using a concept called a Promise.

I recommend familiarizing yourself with JavaScript promises before proceeding further. Best of luck!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason for the failure of this react ternary return statement?

My slideboard is set up to show a warning component (currently just a "test" div) when the prop "columnsItem" exceeds 50. Everything works fine, but when I switch back to a slideboard with fewer columns, all I see is a blank white screen. Can you help me ...

"Adjusting the height of Material UI card content to ensure fixed positioning at the bottom

Currently, I am working on mapping material-ui cards with dynamic content from a JSON object, resulting in varying card heights. I have successfully set a fixed height for each card, but the content is not aligned correctly. You can see the issue in this ...

Utilizing Express.js: Implementing a Route that is Outside the Current Router

app.js: var app = express(); app.use('/my-page', require('./routes/my-page.js')); my-page.js: const router = require('express').Router(); router.get('/one', function (req, res, next) { return res.send('thi ...

Exploring routes with dynamic parameters using Express, MongoDB, and Mongoose

Hello there, I am encountering an issue with my node express mongodb and mongoose server configuration. The routes with parameters seem to be malfunctioning. Is there anything specific that needs to be clarified or added? This is of utmost importance. Tha ...

The functionality of nested routing is not operating properly in react-router

I'm currently struggling to get my CollectionPage to render and match the URL correctly within my nested Route in React. Unfortunately, it doesn't seem to be working as expected! Here's a piece of code from my shop.component file that is be ...

Configuring the text box to utilize jQuery's datepicker feature

Currently, I am facing an issue with setting up a datepicker in a dynamic control creation scenario. The control is being created on the fly, causing inconsistencies with the id, which in turn is preventing the datepicker from functioning properly. Here is ...

Having trouble uploading APK to Google Play Developer using Publisher API

Currently, I am in the process of developing a script using nodejs to upload my APK file to Google Play Developer through the Publishing API. However, I have encountered some issues with the upload process. Despite having a valid APK file, the upload is fa ...

Is there a way to change a .stl file format to .js?

Seeking to create a WebGL-based 3-D viewer, I require access to an imported model in STL (.stl) format. My goal is to convert the STL file to .js for compatibility with web browsers. How can I accomplish this conversion without compromising the integrity ...

Setting up JW Player with a YouTube Embed URL

Embed link Is it possible to embed a YouTube link in my JW Player without disrupting the design? I have a specific design for the JW Player frame that I need to retain while incorporating the YouTube link. I have searched extensively but haven't foun ...

Warning: Unhandled promise rejection - Invalid arguments detected: undefined, string

I was following a tutorial on YouTube titled "Create A Node.js API Authentication Using JWT" but I encountered an error with the /register post request: UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string. Below is the code sni ...

What could be the reason for a code running successfully in node but not in the REPL environment?

This is the current script I'm working with: const lib = require('./lib.js'); const fs = require('fs'); const graph = fs.readFileSync('../js-working-dir/add_graph.pb', 'utf8'); const sess = new lib.Session(gr ...

Is it possible for the ajax url option to accept an array of URLs rather than just one?

Is there a way to retrieve and store data from multiple JSON URLs in an array? <script type="text/javascript"> urls = ["https://spreadsheets.google.com/feeds/list/1RsiDuydBBHyu4OBjlxq1YH6yT3qcJDMB6-YKU-xxd_k/od6/public/basic?hl=en_US&alt=jso ...

jQuery functions do not have any impact on newly appended elements

Why are jQuery functions not affecting appeneded elements after ajax? I am aware of two possible solutions, but neither seems optimal. Firstly, when I append an element, I also re-append the jQuery functions each time. This means that for every appended ...

Modify a JavaScript variable dynamically using an external source

I currently have two files named data1.js and data2.js. In data1.js, there is a variable defined as: var rows = ['g1', 'g2']; Whereas in data2.js, the variable is defined differently as: var rows = ['g1', 'g2', &a ...

Dynamically obtaining the content of a tag using jQuery

Although this question may have been asked multiple times before, I am encountering a peculiar issue. Let me explain the scenario: Within this tag, there is a dynamically loaded integer: <i id="my_id">{{integer value}}</i> I am attempting t ...

Optimal layout for a messaging app: MongoDB reigns supreme

In trying to create a messaging platform similar to Facebook Messenger, there arises a simple design challenge. Imagine John and Mary engaged in conversation - what approach would be more ideal? Option 1: One document per conversation, with an array of me ...

Ways to monitor and measure clicks effectively

Within my website, I have a table element with one column and numerous rows. Each row serves as a hyperlink to an external shared drive. <tr><td ><a href="file://xxx">Staff1</a></td></tr> <tr ><td ><a h ...

Passing image source from parent component to child component in Vue.js

I encountered an issue where I stored the image file name in a variable within the parent component and passed it to the child component using props. However, despite this setup, the child element is not displaying the image as expected. Here is the data ...

The HTML5 thumbs-up feature will not be visible when not logged in

I have searched high and low on the web and even visited #facebook, but with no luck. I have experimented with the FBXML code as well, and it seems that if you are not logged into Facebook, the Like button will not appear on the page. The strange thing is ...

Preventing a user from navigating away from a page without completing a specific action, such as clicking a submit button

I am in the process of developing an interactive quiz platform. The quiz includes a timer that begins counting down once the user initiates the quiz. Upon completing the quiz, the user is expected to submit their answers. If the user runs out of time, th ...