为什么数字记录为NaN?

时间:2019-05-20 11:35:04

标签: javascript

在Google地图上工作。我在地图上从车站对象(带有位置和自行车数)中获取了标记。标记onclick事件显示了自行车的数量。我想显示此数字-1,但返回“ NaN”。

我试图使用ParseInt()和Number()并在网上找到答案。 抱歉,我是一个初学者,答案可能很简单,但我在此问题上受阻。

// My station object


//station tab which will contains all the data
const stationsTab = [];

//Give attributes to station object from data
getInfos().then(function(data){
    data.forEach(function(info){
            const station =
              {
                  stationLocation : info.position,
                  availableBikesNumber : info.available_bikes
              };

        // Put stations in a tab
            stationsTab.push(station);

        // a marker for each station
            mark = new google.maps.Marker
            ({
                map : map,
                position : station.stationLocation,
                bikes : station.availableBikesNumber,
                icon : 'images/bluemarker.png';
});

// It gives me the first number. It runs
document.getElementById("free_bikes").innerHTML = station.availableBikesNumber + "bikes"; // For example station.availableBikesNumber is 9, returns "9 bikes"

// Event when should log number - 1. It doesn't run

submitBtn.addEventListener("click", function(e) {

    document.getElementById("free_bikes").innerHTML = station.availableBikesNumber - 1 + " bikes"; //returns "NaN bikes"
    var temp = Number(station.availableBikesNumber) - 1;
    console.log(typeof station.availableBikesNumber); //returns "undefined
    console.log(Number(temp)); //returns "NaN"
    console.log(typeof temp); //returns "number"
    document.getElementById("free_bikes").innerHTML = temp + " bikes"; //returns "NaN bikes"
  }, false); 

1 个答案:

答案 0 :(得分:0)

我认为这里station可能为null,因为var temp = Number(station.availableBikesNumber) - 1;返回NaN。和

  

NUMBER(temp)

是NaN,因为temp不包含任何数字

请检查您的station变量。