B4J Question RunFunctionWithResult returns an empty variable

Mostez

Well-Known Member
Licensed User
Longtime User
I have a Java script that returns user geolocation, I tested the script with Chrome developer tools and it runs with no problems, the problem is it returns an empty result to B4J server!
Your help is appreciated to solve this problem.
TIA

HTML page:
<button class="btnlocation" type="submit" id="location"  >Location</button>
Java Script:
        function getlocation() {
           // var JSONobj = {};
            if (navigator.geolocation) {
                return new Promise((resolve, reject) => {
                    navigator.geolocation.getCurrentPosition(function (position) {
                        JSONobj = {
                            lat: position.coords.latitude,
                            long: position.coords.longitude,
                            success: true,
                            error: null,
                            message: '',
                        };
                        resolve(JSONobj);
                    }, function (error) {
                        JSONobj = {
                            lat: null,
                            long: null,
                            success: false,
                            error: error.code,
                            message: error.message,
                        };
                        //reject(error);
                        resolve(JSONobj);
                    },
                    //options
                    {
                        maximumAge: 3000,
                        timeout: 60000,
                        enableHighAccuracy: true,
                    });
                });
            } else {
                // Handle error
            }
        }
      
       async  function getGeolocationData() {       
            const retJSONobj = await getlocation();
            console.log(retJSONobj);   //logs OK in console     
            return (retJSONobj);       
            // Use the geolocation data in your application
        }

Chrome Console:
{
    "lat": xx.xx,
    "long": xx.xx,
    "success": true,
    "error": null,
    "message": ""
}

B4J:
Sub Location_Click(Params As Map)
    Dim f As Future  = ws.RunFunctionWithResult("getGeolocationData", Null)
    Log(f.Value)   
    ws.Flush
    'logs these two lines,
    'Waiting for value (100 ms)
    '{}
End Sub
 

Mostez

Well-Known Member
Licensed User
Longtime User
You should wait for the result in JS
if you don't mind, show how to wait for the result? this is a modified version with callback, but is also returns null!

JavaScript:
        function getLocation(successCallback, errorCallback) {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
                    maximumAge: 3000,
                    timeout: 60000,
                    enableHighAccuracy: true,
                });
            } else {
                // Handle error
            }
        }

        function getGeolocationData() {
            // Call the getLocation() function and pass in callback functions.
            getLocation((position) => {
                 //The user's position is available.
                              const JSONobj = {
                                  lat: position.coords.latitude,
                                  long: position.coords.longitude,
                                  success: true,
                                  error: null,
                                  message: "",
                              };
                        const strRetJSONobj = JSON.stringify(JSONobj);
                        console.log(strRetJSONobj); //...OK
                        return ("OK"); //returns null in B4J
            }, (error) => {
                // The user's position could not be determined.
                              const JSONobj = {
                                  lat: null,
                                  long: null,
                                  success: false,
                                  error: error.code,
                                  message: error.message,
                              };
                              const strRetJSONobj = JSON.stringify(JSONobj);
                              console.log(strRetJSONobj); //...OK
                              return ("!OK");  //returns null in B4J
              
            });
            
        }
 
Upvote 0
Top