0

I am getting directory information through ssh and am exporting some details to be stored in db.

// details.js
const SSH = require('simple-ssh');
const config = require('./config');
let count = 0;  
let update; 
const ssh = new SSH({
        host: config.host,
        user: config.user,
        pass: config.pass});
ssh.start(); 
getInfo(config.dirpath);

function getInfo(dir){
return new Promise(function(resolve,reject){
    ssh
        .exec(`date '+%F %r' -r  ${dir}`,{
            out: function (stdout) 
            {  update = stdout
            },
            err: function (stderr) { console.log(stderr); 
            } 
            })
        .exec(`ls ${dir}/ ` ,{
              out: function (stdout) 
             {stdout = stdout.split("\n"); 
               for (var i =0; i < stdout.length; i++){
                childinfo(dir+"/"+stdout[i]);
            }},
            err: function (stderr) { console.log(stderr); } 
        })  
     resolve(update)
  )}     
}

function childinfo(dirName){
 return new Promise(function(resolve,reject){
    ssh
      .exec(`ls  ${dirName} | wc -l `,{
        out: function (stdout) 
        {  count = count + parseInt(stdout);
           resolve(count);
      },
        err: function (stderr) { console.log(stderr);}
    })
})
}
module.exports ={
   getInfo,
   childInfo
}

The values are exported before it can be updated. I tried using async-await and Promises. Following this.

I am getting reject message even though all console.logs are being executed properly.

I am new to javascript and hence not able to use async-await properly.

//export.js
const fileInfo = require('./details.js');
const config = require('./config');

async function getAllDetails(){
   let d = await fileInfo.getInfo(config.path);
   let c = await fileInfo.childInfo();
   return [d,c];
}

(async function(){
       let info = await getAllDetails();
       console.log(info[1]);
})();
  • 1
    Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Sebastian Simon Jun 24 '21 at 14:48
  • @SebastianSimon are you sure about that duplicate ? The variables are defined in the correct scope. It seems to be an issue about module exporting. `count` should at least be "0", not undefined. – Seblor Jun 24 '21 at 14:49
  • Did you check what was the value of `fileInfo` ? – Seblor Jun 24 '21 at 14:49
  • Rather than returning those values i.e. count and update return the functions that return those values, and wait for them to finish executing. Use async await, or callbacks. You are exporting the values, and those values are being modified later after function finishes executing. – sujeet Jun 24 '21 at 14:50
  • @Seblor count is not undefined, its 0. Like I said the using SetTimeout I am able to import values fileInfo.count and fileInfo.update. – user16307445 Jun 24 '21 at 14:56
  • I never said that `count` is `undefined`. The duplicate target is about variables being _unaltered_ when modified asynchronously in a function, which almost certainly happens here as well. – Sebastian Simon Jun 24 '21 at 14:58
  • @SujeetAgrahari I have commented out the exporting function method, even that doesn't work. My only workaround is using setTimeout for both module.exports and in import file for values to be loaded and its not efficient. – user16307445 Jun 24 '21 at 14:58
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. `update` etc. are modified *asynchronously*, that is why `setTimeout` "works": enough time has passed that the async call has completed. – Dave Newton Jun 24 '21 at 17:48
  • "*I tried using async-await and Promises.*" - please show us *what* you tried. Otherwise we can only refer you to the canonical questions. – Bergi Jun 24 '21 at 18:43
  • @DaveNewton Hey thanks for the link, its still returning undefined. I don't know where I am going wrong. Don't know how to resolve the second function as its being called from the first function. I have edited the code. – user16307445 Jun 24 '21 at 18:51
  • @Bergi hey, I tried similar variations of what I have edited, just not with IIFE. – user16307445 Jun 24 '21 at 19:01
  • 1
    You cannot just replace the `return update` with `resolve(update)` - the variable still isn't assigned when that line runs. Instead, you need to place `resolve(stdout)` instead of the `update = stdout`. – Bergi Jun 24 '21 at 19:11
  • The `childinfo` needs to be promisified as well – Bergi Jun 24 '21 at 19:12
  • @Bergi Thanks a lot. I am able to get values from `getInfo()`. The parameters for `childinfo` is passes from `getInfo()` and I am unable to import that. – user16307445 Jun 24 '21 at 19:28

0 Answers0