0

I have set up a websocket that reads all e-mail addresses as a backend and sends them to the client. Now I want the used memory space to be sent along with the quota. The mysql-Query results in an array with several JSON strings (results):

{ id: 10,
    domain_id: 6,
    email: 'mailadress',
    password: 'password',
    quota: 5368709120,
    crypt: 2,
    wsplx: 'customer',
    quota_used: undefined }

As a further value I want to enter “quota_used.” That’s why I wrote the following code:

for (value of results) {
  console.log(get_used_quota(value.email))
  value.quota_used = get_used_quota(value.email);
}
socket.emit("response_get_update_mails", (results));

function get_used_quota(email) {
  var mailadress_arr = email.split("@");
  var cmd = "du -sb /var/vmail/" + mailadress_arr[1] + "/" + mailadress_arr[0] + "/Maildir/";
  exec(cmd, (error, stdout, stderr) => {
    if (error) {
      showtoaster("\"" + `${error.message}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
      return;
    }
    if (stderr) {
      showtoaster("\"" + `${stderr}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
      return;
    }
    var output = `${stdout}`;
    var size_arr = output.split("\t");
    size = parseInt(size_arr[0]);
    return size
  })
}

The problem is that I can’t get the value “size” out of the arrow function. The return value of the function remains undefined. Can someone help me to fix this problem, this would be very nice? Let my know, if you need more information.

jabaa
  • 5,844
  • 3
  • 9
  • 30
Ben
  • 1
  • 1
  • I'm unfamiliar with node.js but it looks like the return value of the arrow func is not captured let alone returned by the get_used_quota function, which I'm gonna assume is the one you're expecting a return value from. as it is the named function is just doing a thing and then returning nothing. – WdeVlam Sep 19 '21 at 11:44
  • Assume that the exec command discards any value returned from the callback it makes after running the shell command asynchronously. Asynchronous coding techniques will be needed to delay callling `socket.emit` until after call backs supplyting size data have been called. Do you also need to wait until all values in `results` have been processed and only emit `results` once? – traktor Sep 19 '21 at 11:47

1 Answers1

0

You can wrap that call in a promise:

function get_used_quota(email) {
  var mailadress_arr = email.split("@");
  var cmd = "du -sb /var/vmail/" + mailadress_arr[1] + "/" + mailadress_arr[0] + "/Maildir/";
  return new Promise(resolve => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        showtoaster("\"" + `${error.message}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
        return;
      }
      if (stderr) {
        showtoaster("\"" + `${stderr}` + "\"", "Ein Fehler ist aufgetreten!", "warning", true, true, 3000)
        return;
      }
      var output = `${stdout}`;
      var size_arr = output.split("\t");
      size = parseInt(size_arr[0]);
      resolve(size);
    });
  });
}

and await the result

value.quota_used = await get_used_quota(value.email);
jabaa
  • 5,844
  • 3
  • 9
  • 30
  • Hello! Thank for helping. VS code says: "await is only valid in async functions and the top level bodies of modules. How can I avoid this crash? – Ben Sep 19 '21 at 13:48
  • @Ben Mark the outer function with `async` (the function `value.quota_used = await get_used_quota(value.email);` is in). – jabaa Sep 19 '21 at 15:12
  • Hey, I added a new response to my question to show you the new code formatted (with async). It is still not working. Should I change something? – Ben Sep 19 '21 at 16:50
  • 1
    @Ben You shouldn't mark `get_used_quota` with `async` but the function that contains the call `value.quota_used = await get_used_quota(value.email);` – jabaa Sep 19 '21 at 17:02
  • Okay, that makes sense: Now, when I hit console.log(get_used_quota(value.email)), the output is "Promise { }. – Ben Sep 19 '21 at 17:15
  • @Ben You forgot the `await`. See the last line in my answer. – jabaa Sep 19 '21 at 17:17