I am working on web ui
project based on this project. Now what I want to do is that when a user opens the webpage, the ip address
of the device from which this page is opened should be returned. For this I added the below code to the javascript` code here:
function myIP() {
$.getJSON("//freegeoip.net/json/?callback=?", function(data) {
const ipInformation = JSON.parse(data);
console.log(ipInformation.ip);
return ipInformation.ip;
});
}
const sessionAttr = myIP();
I also added <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
at the top of the same javascript
code file. Now sessionAttr
constant is called at line 60 of this code file (Note that I changed sessionAttributes: config.lex.sessionAttributes
to sessionAttributes: config.sessionAttr
).
When I try to load this page nothing shows up. if I do not make changes I described above then the page loads up correctly. So somehow I am making some mistake in my additions which is screwing this page.
NOTE: I am not at all familiar with JavaScript
but based on a quick search I made the changes described above. I understand the issue is in the asynchronous call I am making and I went through this suggested link but I am unable to figure out the right structure. Can anyone provide me the right syntax so that the page loads up correctly and also returns the ip address
of the client and sets it to sessionAttribute
?
UPDATE: After some suggestions I made following changes to my code (link here - https://jsfiddle.net/ywdeu0o4/3/)
const configDefault = {
Bot: {
// initial sessionAttributes
sessionAttributes: {},
},
};
$(document).ready(function(){
configDefault.Bot.sessionAttributes.ip = myIP();
// undefined
});
function myIP() {
$.getJSON("//freegeoip.net/json/?callback=?", function(data) {
//console.log(data.ip);
return data.ip;
});
}
console.log("myVar:",configDefault.Bot.sessionAttributes.ip);
When I run this code after opening the console I get undefined
value for configDefault.Bot.sessionAttributes.ip
. Why is it coming as undefined and not the ip address?