0

So, I'm trying to return the stdout from a command that I want to execute using exec. However, since exec is async I can't seem to return the stdout before the response sends an undefined output. Since I am working withing a router.post function (if that's even the name), the callback and async/await don't seem to help me.

Here is the code I use for the execution.

router.post("/select", (req, res, next) => {
  const imageLocation = "backend/images/" + req.body.imagePath;
  const execString = 'python backend/volatility/vol.py -f ' + imageLocation + ' imageinfo | grep "Suggested Profile(s) :"'
  let output;
  exec(execString, (err, stdout, stderr) => {
    if (err) {
      console.log(err);
    }
    output = stdout;
    console.log(stdout);
  });
    console.log(output);
    res.status(200).json({
      message: "test",
      output: output
    })
  console.log(req.body.imagePath);
})

What I want to have happen is a response that sends the output after it has been set by exec

I might be overlooking something obvious, but I'm just not seeing it. I've looked everywhere and can't seem to find an answer.

EDIT:

Sorry to the people who already responded so fast. I'm trying to get the response over to angular and I think I might be doing something wrong over there.

This is my log.service.ts with the relevant function:

  setImage(imagePath: any) {
    let response;
    this.http.post("http://localhost:3000/api/log/select", imagePath).subscribe((responseData) => {
      response = responseData;
      return response;
    });
  }

And here is my component for use with the html template:

  imageChosen(event: any) {
    console.log({imagePath: event.value});
    this.suggestedProfile = this.logService.setImage({imagePath: this.selectedImage});
    console.log(this.suggestedProfile);

  }

Maybe it has something to do with the subscribe() part? I'm sorry if I caused confusion!

Roel Huizing
  • 3
  • 1
  • 3
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Marcos Casagrande Mar 28 '19 at 17:12

2 Answers2

2

You can try to change the code like this

router.post("/select", (req, res, next) => {
  const imageLocation = "backend/images/" + req.body.imagePath;
  const execString = 'python backend/volatility/vol.py -f ' + imageLocation + ' imageinfo | grep "Suggested Profile(s) :"'
  let output;
  exec(execString, (err, stdout, stderr) => {
    if (err) {
      console.log(err);
    } else {
      output = stdout;
      console.log(stdout);
      res.status(200).json({
        message: "test",
        output: output
      })
      console.log(req.body.imagePath);
    }
  });
})
Alexey Zelenin
  • 710
  • 4
  • 10
  • I tried this, but it didn't change anything significant. It's my mistake, I should have explained my problem better. I have updated the original post. Thanks for helping anyway :) – Roel Huizing Mar 28 '19 at 17:45
0

In this case:

setImage(imagePath: any) : Observable<string> {
    return(this.http.post("http://localhost:3000/api/log/select", imagePath));
  }

imageChosen(event: any) {
    console.log({imagePath: event.value});
    this.logService.setImage({imagePath: this.selectedImage}).subscribe((response) => {
        this.suggestedProfile = response;
        console.log(this.suggestedProfile);
    });
  }
  1. return not value but observable
  2. subscribe and use the value there
Alexey Zelenin
  • 710
  • 4
  • 10