JavaScript-Console.log返回某些内容,但返回时显示未定义

JavaScript-Console.log返回某些内容,但返回时显示未定义,javascript,return,console.log,Javascript,Return,Console.log,正如标题所说的问题:console.log显示正确的值,但是当我返回它并通过在段落中添加段落(使用jQuery)用DOM显示它时,它会显示未定义的 console.log(dateTimeUTC(timeZoneStr)); // Shows the correct value I wanted return dateTimeUTC(timeZoneStr); // Shows undefined 所以我想做的是:一个带有html的表单,带有一个简单的输入文本和一个提交按钮,当单击提交

正如标题所说的问题:console.log显示正确的值,但是当我返回它并通过在段落中添加段落(使用jQuery)用DOM显示它时,它会显示未定义的

console.log(dateTimeUTC(timeZoneStr)); 
// Shows the correct value I wanted

return  dateTimeUTC(timeZoneStr); 
// Shows undefined
所以我想做的是:一个带有html的表单,带有一个简单的输入文本和一个提交按钮,当单击提交按钮时,它会使用jQuery将输入文本保存在一个变量中:
let variable=$('#cityName').val()(该值应为城市名称)因为当我获得城市名称值时,我要求openweathermap API向我发送json并以毫秒为单位给出时区值,然后我需要以小时为单位转换毫秒值,例如纽约:时区:-14400(
json.timezone/60/60)
因此纽约的结果是UTC-4,然后我有一个脚本,它可以在一个实时日期时间转换我的UTC数据,但是这个脚本可以工作,所以我不需要向您解释。。。当该函数完成时,它会给出如下结果:

2019年8月13日星期二05:53:39 GMT+0200 不太适合演示,因此我制作了一个函数,以更好的方式进行转换:

然后这个返回值转到我执行函数的地方,在名为timeZone的函数中,我让console.log在那里工作,但返回时显示未定义

希望我解释得很好,如果有人能给我一些帮助,你会明白我在做什么^_^

编辑-完整代码:

Index.html代码:

<div  class="form-group">
    <label  for="cityName">Displays the local date and time according to the name of the city</label>
    <input  name="cityName"  type="text"  id="cityName"  placeholder="Enter a name of a city..."  class="form-control">
</div>
<br>
<button  type="submit"  id="submit">Submit</button>
<p  class="results"></p>

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script  src="./scripts/main.js"></script>

好的,有人帮我找出错误的原因,下面是解决方案: 由于我们无法知道请求何时结束,因此无法在getJSON之外返回值,您需要将所需内容直接附加到getJSON中,因此main.js中的代码如下:

$( "#submit" ).click(function()
{
let  city  =  $('#cityName').val();
let  cityName  =  city.split(' ').join('+');
if(cityName  ===  "")
{
    $('.results').append("<p>Wrong location.</p>");
}
else
{
   weatherRequest("http://api.openweathermap.org/data/2.5/weather?q="  +  cityName  +  "&units=metric&appid=MyAPIkey");
}
});
$(“#提交”)。单击(函数()
{
设city=$('#cityName').val();
让cityName=city.split(“”).join(‘+’);
如果(城市名称==“”)
{
$('.results')。追加(错误位置。

); } 其他的 { 天气预报请求(”http://api.openweathermap.org/data/2.5/weather?q=“+cityName+”&units=metric&appid=MyAPIkey”); } });
只需更改功能weatherRequest:

function  weatherRequest(url) {
// console.log(url);
try
{
    $.getJSON(url, function  callbackSuccess(json) {
let  showDateTimeValue  =  timeZone(json);
let  city  =  json.name;
    $('.results').append("<p>The date and time of "  +  city  +  " : "  +  showDateTimeValue  +  '</p>');
});
}
catch
{
    $('.results').append("<p>Wrong location.</p>");
}
}
函数weatherRequest(url){
//console.log(url);
尝试
{
$.getJSON(url,函数callbackSuccess(json){
让showDateTimeValue=时区(json);
让city=json.name;
$('.results')。追加(“城市+”:“+showDateTimeValue+”

”)的日期和时间; }); } 抓住 { $('.results')。追加(错误位置。

); } }

希望你能理解,我不擅长解释,所以是的…

好吧,有人帮我找出错误的原因,下面是解决方案: 由于我们无法知道请求何时结束,因此无法在getJSON之外返回值,您需要将所需内容直接附加到getJSON中,因此main.js中的代码如下:

$( "#submit" ).click(function()
{
let  city  =  $('#cityName').val();
let  cityName  =  city.split(' ').join('+');
if(cityName  ===  "")
{
    $('.results').append("<p>Wrong location.</p>");
}
else
{
   weatherRequest("http://api.openweathermap.org/data/2.5/weather?q="  +  cityName  +  "&units=metric&appid=MyAPIkey");
}
});
$(“#提交”)。单击(函数()
{
设city=$('#cityName').val();
让cityName=city.split(“”).join(‘+’);
如果(城市名称==“”)
{
$('.results')。追加(错误位置。

); } 其他的 { 天气预报请求(”http://api.openweathermap.org/data/2.5/weather?q=“+cityName+”&units=metric&appid=MyAPIkey”); } });
只需更改功能weatherRequest:

function  weatherRequest(url) {
// console.log(url);
try
{
    $.getJSON(url, function  callbackSuccess(json) {
let  showDateTimeValue  =  timeZone(json);
let  city  =  json.name;
    $('.results').append("<p>The date and time of "  +  city  +  " : "  +  showDateTimeValue  +  '</p>');
});
}
catch
{
    $('.results').append("<p>Wrong location.</p>");
}
}
函数weatherRequest(url){
//console.log(url);
尝试
{
$.getJSON(url,函数callbackSuccess(json){
让showDateTimeValue=时区(json);
让city=json.name;
$('.results')。追加(“城市+”:“+showDateTimeValue+”

”)的日期和时间; }); } 抓住 { $('.results')。追加(错误位置。

); } }

希望你能理解,我不善于解释,所以是的…

console.log(dateTimeUTC(timeZoneStr))之后您是否尝试了
console.log(timeZoneStr)?可能在log和return之间更改了字符串是的,我刚刚测试了它,结果如下:console.log(timeZoneStr);//输出:-4(当然在控制台中)返回(timeZoneStr);//输出:段落中未定义,如果有帮助,这里是函数$(“#提交”)的执行代码。单击(函数(){let city=$(''#cityName').val();cityName=city.split('').join('+');if(cityName==''){$('.results').append(错误位置。

);否则{$('.results')。append(“La date et l'heure de”+city+:“+weatherRequest”(“在您的注释中的代码中,哪个函数是返回dateTimeUTC值的函数?(函数的名称是什么?)另外,你能用这段代码更新你的问题吗?好的!谢谢你对我的帮助。我是新来堆栈溢出的,在
console.log(dateTimeUTC(timeZoneStr));
你试过
console.log(timeZoneStr)吗;
?可能在log和return之间更改了字符串是的,我刚刚测试了它,结果如下:console.log(timeZoneStr);//Output:-4(当然在控制台中)return(timeZoneStr);//Output:段落中未定义,如果有帮助,下面是执行函数$(“#submit”)的代码。单击(function(){let city=$('#cityName').val();cityName=city.split('').join('+');if(cityName===''){$('.results')。append(位置错误。

);else{$('.results')。append(La date et l'heure de“+city+”:“+weatherRequest(“在您评论中的代码中,哪个函数是返回dateTimeUTC值的函数?(该函数的名称是什么?)此外,您能否使用此代码更新您的问题,以便更清楚地看到?完成!感谢您的帮助。顺便说一句,我是堆栈溢出新手。”^^
function  weatherRequest(url) {
// console.log(url);
try
{
    $.getJSON(url, function  callbackSuccess(json) {
let  showDateTimeValue  =  timeZone(json);
let  city  =  json.name;
    $('.results').append("<p>The date and time of "  +  city  +  " : "  +  showDateTimeValue  +  '</p>');
});
}
catch
{
    $('.results').append("<p>Wrong location.</p>");
}
}