1

I'm working on a project in JS(Electron,Node.js)that needs to check if a key in the Windows registry is present. Here is my code:

function IsSettedUp() {
regedit.list('HKCU\\SOFTWARE')
    .on('data', function (entry) {
        //Returns Keys
        console.log(entry.data.keys)

        //Checks if "WinXSoft" appears in the entry.data.keys array
        var key = $.inArray("WinXSoft", entry.data.keys)

        console.log(key)

        //Returns false if WinXSoft wasn't found
        if (key == -1) {

            return false
        }
        //Returns true if WinXSoft was found
        else {
            return true
        }
    })

    }
    //Should be true or false according to the WinXSoft key
    var z = IsSettedUp();
    console.log(z)

When I create the key and run the code,this is the output:

console.log(entry.data.keys) outputs Array(42).(Expected)

console.log(key) ouputs 13(Expected)

console.log(z) outputs undefined(???,Should be true)

So yeah,do you guys know how I can fix this?

  • 1
    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) – Evan Trimboli Sep 30 '18 at 10:55

4 Answers4

1

Try to return value out of the on function

function IsSettedUp() {
     var result = false;
     regedit.list('HKCU\\SOFTWARE').on('data', function (entry) {
        //Returns Keys
        console.log(entry.data.keys)

        //Checks if "WinXSoft" appears in the entry.data.keys array
        var key = $.inArray("WinXSoft", entry.data.keys)

        console.log(key)

        //Returns false if WinXSoft wasn't found
        if (key == -1) {

            result = true;
        }
        //Returns true if WinXSoft was found
        else {
            result = false;
        }
    });
    return result;
 }
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • This still returns undefined –  Sep 30 '18 at 11:11
  • @libert1quinten then you can init `result` with a default value and check your code logic – flyingfox Sep 30 '18 at 11:13
  • If I init "result" with the value of 1 it returns 1 but not true or false and I know for sure that my if/else statements are correct –  Sep 30 '18 at 11:16
  • There is still no *return* statement for the *IsSettedUp* function here. All the *return* statements are for the anonymous function that the *on* method takes. – 3limin4t0r Sep 30 '18 at 11:20
  • @JohanWentholt thanks for your comment,and updated,it's my careless – flyingfox Sep 30 '18 at 11:21
  • 1
    @libert1quinten Updated my answer according to Johan's comment,you can check now – flyingfox Sep 30 '18 at 11:22
  • @Johan Wentholt I did try to return the result variable outside of the on method but it stills returns undefined.It's like that the result=false and the result = true don't have an effect on the result function. –  Sep 30 '18 at 11:23
  • 1
    @libert1quinten If you give the variable a default value like is done in the answer and return it outside of the function provided to the *on* method. It should always return `false` (default value in this answer) if the provided *on* function is called asynchronously. Since the method setting the return values is executed after the *IsSettedUp* function returns. For more info about asynchronous return values see the possible duplicate link under the answer. If the function provided to *on* is called synchronously this answer should resolve your issue. – 3limin4t0r Sep 30 '18 at 11:32
  • Oops, I mean duplicate link under the **question**. – 3limin4t0r Sep 30 '18 at 11:42
0

that is because z is the value of IsSettedUp return , which is not there and hence undefined.

true is the return value for the callback provided to on function.

function IsSettedUp() {
regedit.list('HKCU\\SOFTWARE')
    .on('data', function (entry) {
        //Returns Keys
        console.log(entry.data.keys)

        //Checks if "WinXSoft" appears in the entry.data.keys array
        var key = $.inArray("WinXSoft", entry.data.keys)

        console.log(key)

        //Returns false if WinXSoft wasn't found
        if (key == -1) {

            return false
        }
        //Returns true if WinXSoft was found
        else {
            return true
        }
    })
    return 'test' // ADD THIS RETURN
    }
    //Should be true or false according to the WinXSoft key
    var z = IsSettedUp();
    console.log(z)

try this edited code and see what the value of z comes out now, one line is added at the end of your function

ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

True or false is returned for inside function (entry) {. Nothing is returned for outside function IsSettedUp() {, so return value is the default undefined.Formatting code could make function scope clear.

leaf
  • 1,624
  • 11
  • 16
0

I didn't found a solution for this problem so I decided to us another library that works fine now.