TypeError:undefined不是对象(评估' type.toString')

时间:2016-02-01 01:32:22

标签: javascript forms function typeerror var

过去三个小时里,我一直在努力解决这个问题。我不知道我还能尝试什么。我希望在某个按钮" id"时设置var undecidedType, undecidedFilter。点击。我不知道我做错了什么。也许有人可以帮忙。

这是我的.js:

function getLocation() {
Modernizr.geolocation ? navigator.geolocation.getCurrentPosition(currentLocation, handle_error, {
    timeout: 1e4
}) : ($wait.fadeOut(), $locationBar.fadeIn())
}
function handle_error(e) {
$locationBar.css("opacity", 1), showError("Location not found"), 0 == e.code, 1 == e.code, 2 == e.code, 3 == e.code
}
function currentLocation(e) {
$wait.fadeIn(), $locationBar.fadeOut();
var o = e.coords.latitude, t = e.coords.longitude;

var undecidedType, undecidedFilter;

var b1 = document.getElementById('rest'), b2 = document.getElementById('bar');

b1.onclick = function() {
    undecidedType = "restaurant";
    undecidedFilter = "restaurant";
};
b2.onclick = function() {
  undecidedType = "bars";
  undecidedFilter = "pubs";
};
currentlatlng = new google.maps.LatLng(o, t), getPlaces(currentlatlng, undecidedType)
}
function getPlaces(e, type) {
userLoc = e, homeMarker = new google.maps.Marker({
    map: map,
    animation: google.maps.Animation.DROP,
    position: e,
    icon: homeIcon
});
var o = {
    location: e,
    radius: 1e3,
    rankby: "prominance",
    keyword: type.toString()
};
service.search(o, storeRequestBar)
}
function storeRequestBar(e, undecidedFilter) {
barResultsStore = e;
var o = {
    location: currentlatlng,
    radius: 1e3,
    rankby: "prominance",
    keyword: undecidedFilter.toString()
};
service.search(o, storeRequestPub)
}
function storeRequestPub(e) {
for (pubResultsStore = e, totalResults = barResultsStore.concat(pubResultsStore), resultsStore = removeDupes(totalResults, "id"), resultsStore = resultsStore.sort(function() {
    return Math.random() - .5
}), i = 0; i < resultsStore.length; i++);
0 == resultsStore && showError("Cannot find anything in that location. Try somewhere else"), chooseBar(resultsStore)
}
function chooseBar(e) {
barRef = {
    reference: e[shitCounter].reference
}, service.getDetails(barRef, showBar)
}
function showBar(e, o, hello) {
if (o == google.maps.places.PlacesServiceStatus.OK) {
    for (i in markersArray)
        markersArray[i].setMap(null);
    drinkMarker = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        position: e.geometry.location,
        icon: drinkIcon
    }), markersArray.push(drinkMarker), placeName = e.name, calcRoute(userLoc, e.geometry.location), directionsDisplay.setMap(null), directionsDisplay.suppressMarkers=!0, directionsDisplay.polylineOptions = {
        strokeColor: "#ff8400",
        strokeOpacity: .8,
        strokeWeight: 5
    }, directionsDisplay.setMap(map), placeSite = e.website ? e.website : e.url, placeAddress = e.formatted_address, $(".recommendation__destination").html("Why don't you go to <br/><a href='" + placeSite + "' target='_blank'>" + placeName + "</a>" + currentLocation.hello + ""), $(".map__address").html(placeAddress), $(".grid__row--links,.grid__row--actions,.fyyfs,.recommendation,.map__address,.recommendation__destination,.adsense").fadeIn(function() {
        $wait.fadeOut()
    })
}
}
function calcRoute(e, o) {
var t = {
    origin: e,
    destination: o,
    travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(t, function(e, o) {
    o == google.maps.DirectionsStatus.OK && directionsDisplay.setDirections(e)
})
}
function codeAddress() {
$wait.fadeIn(function() {
    $locationBar.fadeOut();
    var e = document.getElementById("locationsearch").value;
    geocoder.geocode({
        address: e
    }, function(e, o) {
        o == google.maps.GeocoderStatus.OK ? (currentlatlng = e[0].geometry.location, getPlaces(currentlatlng)) : showError("Location not found. Try again")
    })
})
}
function showError(e) {
$wait.fadeOut(), $locationBar.fadeIn(), $(".locator__message").text(e).fadeIn()
}
function removeDupes(e, o) {
var t = [], r = {};
for (var a in e)
    r[e[a][o]] = e[a];
for (a in r)
    t.push(r[a]);
return t
}
var resultref, marker, markersArray = [], shitCounter = 0, iteration = 0, drinkIcon = "img/restaurant_map_pointer_small-2.png", homeIcon = "img/current_location_map_pointer_small-2.png", resultsStore, totalResults = [], pubResultsStore, barResultsStore, userLoc, currentlatlng, undecidedType, undecidedFilter;
$wait = $(".loader"), $locationBar = $(".locator"), $mapCanvas =     $(".map__canvas"), $type = $(".type");
var lowSat = [{
featureType: "all",
stylers: [{
    saturation: 1,
    strokeColor: "red",
    strokeOpacity: .8,
    strokeWeight: 5
}
]
}
], myOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: lowSat,
mapTypeControl: !1,
panControl: !1,
zoomControl: !0,
mapTypeControl: !1,
scaleControl: !1,
streetViewControl: !1,
overviewMapControl: !1
};
map = new google.maps.Map(document.getElementById("googlemap"), myOptions), geocoder = new google.maps.Geocoder;
var service = new google.maps.places.PlacesService(map);
$(document).ready(function() {
var e = window.location.href, o = e.split("?");
"undcd" == o[1] ? $locationBar.css("opacity", 1) : getLocation()
});
var directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: !0
}), directionsService = new google.maps.DirectionsService;
$(".locator").on("submit", function(e) {
codeAddress(), e.preventDefault()
}), $(".actions__shit").click(function() {
return shitCounter < resultsStore.length - 1 ? shitCounter++ :  shitCounter = 0, chooseBar(resultsStore), !1
});
var autoOptions = {
types: ["geocode"]
}, autoInput = document.getElementById("locationsearch");
autocomplete = new google.maps.places.Autocomplete(autoInput,  autoOptions);

这是html表单:

<form class="form" class="locator" action="#">
      <button id="rest" class="btn" type="submit" value="restaurant">two</button><br>
      <button id="bar" class="btn" type="submit" value="bar">one</button><br>
    </form>

这是错误:

TypeError: undefined is not an object (evaluating 'type.toString')
getPlacesapp-min.js:37
currentLocationapp-min.js:24

1 个答案:

答案 0 :(得分:0)

首次致电type时,

getPlaces未定义,因为当用户点击特定按钮时会定义undecidedType

在getPaces中更改此行:

keyword: type.toString()

keyword: type ? type.toString() : ""

或者将默认值设置为undecidedType

要修复点击侦听器,请将其更改为:

b1.onclick = function() {
        undecidedType = "restaurant";
        undecidedFilter = "restaurant";
        currentlatlng = new google.maps.LatLng(o, t); 
        getPlaces(currentlatlng, undecidedType);
};

b2.onclick = function() {
    undecidedType = "bars";
    undecidedFilter = "pubs";
    currentlatlng = new google.maps.LatLng(o, t);
    getPlaces(currentlatlng, undecidedType)
};