Exported and required function not returning any values.


  • I have two files app.js and drc-functions.js, I'm trying to keep all my functions in the drc-functions.js export them and require them in app.js. The functions work as expected if I define them in the app.js file so I know it's not the functions. If I try to call them from the drc-functions.js they do not return any values. I also noticed that I need to declare the array in both files in order for it to be defined, I thought declared objects outside a function where global? Any suggestions on what I'm doing wrong would be greatly appreciated.

    This is a snip-it of code from drc-functions.js that defines a function.

    
    let dogStatus = [];
    
    function getDogStatus() {
      var sql = "select ds_desc from dog_status where ds_active = 'Y' order by 1;";
      con.query(sql, (err, rows) => {
        dogStatus = [];
        if (err) throw err;
        for (var i = 0; i < rows.length; i++) {
          dogStatus.push(rows[i].ds_desc);
        };
         return dogStatus;
      });
    };
    
    module.exports = {
      getDogStatus,
      getClientStatus
    }
    
    

    This is from the app.js file.

    const myFunctions = require('./drc-functionsc.js');
    
    let dogStatus = [];
    
    app.get('/dropdown', function(req, res) {
      dogStatus = myFunctions.getDogStatus();
      res.render('pages/array-drop', {
        dogStatus: dogStatus,
      });
    });
    
    


  • Can you try the following:

    let dogStatus = [];
    
    function getDogStatus() {
      return new Promise(resolve => {
      var sql = "select ds_desc from dog_status where ds_active = 'Y' order by 1;";
      con.query(sql, (err, rows) => {
        dogStatus = [];
        if (err) throw err;
        for (var i = 0; i < rows.length; i++) {
          dogStatus.push(rows[i].ds_desc);
        };
         resolve(dogStatus);
      });
    });
    };
      
    
    module.exports = {
      getDogStatus,
      getClientStatus
    }
    

    In the app.js

    const myFunctions = require('./drc-functionsc.js');
    
    let dogStatus = [];
    
    app.get('/dropdown', async function(req, res) {
      dogStatus = await myFunctions.getDogStatus();
      res.render('pages/array-drop', {
        dogStatus: dogStatus,
      });
    });
    


  • What do you get when you console log dogStatus right before you return it in getDogStatus() ? Any other messages (errors) in the backend log that might tell you what is going on?



  • If I add console.log(dogStatus[i]); after dogStatus.push(rows[i].ds_desc); in the function , I get the list of values.



  • Can you try the following:

    let dogStatus = [];
    
    function getDogStatus() {
      return new Promise(resolve => {
      var sql = "select ds_desc from dog_status where ds_active = 'Y' order by 1;";
      con.query(sql, (err, rows) => {
        dogStatus = [];
        if (err) throw err;
        for (var i = 0; i < rows.length; i++) {
          dogStatus.push(rows[i].ds_desc);
        };
         resolve(dogStatus);
      });
    });
    };
      
    
    module.exports = {
      getDogStatus,
      getClientStatus
    }
    

    In the app.js

    const myFunctions = require('./drc-functionsc.js');
    
    let dogStatus = [];
    
    app.get('/dropdown', async function(req, res) {
      dogStatus = await myFunctions.getDogStatus();
      res.render('pages/array-drop', {
        dogStatus: dogStatus,
      });
    });
    


  • Avan, You are a lifesaver, that worked like a charm. I looked at some examples of promises online and they didn't make sense to me but now I can see it more clearly.

    Thank you so much.



  • Glad I could be of help. Do you mind selecting my last reply the correct answer? Thank you.