Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How does this function returns null?

If I use function lsinfo() without async the code works fine but info['_myip'] return null.

when I use function lsinfo() with async then info['_myip'] return data but lsinfo() retuen 0 length

What I have tried:

JavaScript
function get_app_data() {

    var json_obj = lsinfo();
    var myJSON = JSON.stringify(json_obj);
}

async function lsinfo() {
    if (navigator.userAgent) {
        localStorage.lsuserAgent = navigator.userAgent;
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayLocationInfo);
    }

    var lsuser_id = localStorage.getItem("user_id")

    if (lsuser_id === null || lsuser_id.length === 0) {
        lsuser_id = "";
    }

    var info = new Object();
    info['_luserid'] = lsuser_id;
    info['_mypage'] = localStorage.getItem("lspage");
    info['_myip'] = localStorage.getItem("gioloc");
    info['_divid'] = localStorage.getItem("lsdvicid");
    info['_appver'] = localStorage.getItem("lsappver");
    info['_useragent'] = localStorage.getItem("lsuserAgent");
    info['_mymsg'] = "";
    return info;
}


function displayLocationInfo(position) {
    const lng = position.coords.longitude;
    const lat = position.coords.latitude;
    localStorage.gioloc = lat + ',' + lng;
}
Posted
Updated 27-Jun-22 1:10am
v2

1 solution

Quote:
If I use function lsinfo() without async the code works fine but info['_myip'] return null.
The async keyword does nothing in your context because you are not using an await keyword in the function scope. You say that it returns null, the only plausible reason that I could think of — remember that I cannot access your machine, so debugging is a bit hard — is that your localStorage.getItem("gioloc"); returns a null object. Meaning, the item with key gioloc does not exist "yet" in your localStorage.

Quote:
when I use function lsinfo() with async then info['_myip'] return data but lsinfo() retuen 0 length
This is very unclear sentence. Async will not change the return type or the value of the return — other than making it a promise[^] that can be awaited, if anything. Secondly, your lsinfo is a method that you need to call. Applying the length to the return would provide the length of the return value (which in your method is a new Object() with some added attributes). Perhaps, you want to do lsinfo()['_myip'].length? Continue reading for a potential fix, I think you are not using the localStorage API correctly.

JavaScript
localStorage.gioloc = lat + ',' + lng;
Also, this is not how the localStorage API is supposed to be used. Here you are setting a field to the localStorage object "itself". To set a key in the localStorage you need to use the setItem function.
JavaScript
function displayLocationInfo(position) {
    const lng = position.coords.longitude;
    const lat = position.coords.latitude;
    localStorage.setItem("gioloc", lat + ',' + lng);
}
This should set the item and the next time you access it, you will be able to read the value.

Window.localStorage - Web APIs | MDN[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900