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:
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;
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.
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.
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
[
^
]
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.