V
V
VPVPVP2020-05-10 23:47:16
JavaScript
VPVPVP, 2020-05-10 23:47:16

The error Uncaught TypeError: Cannot read property 'predictions' of undefined takes off how to fix in my case?

Here is the function itself

function makeSearchGoogle() {
    var searchInput = $('.search-input').val();
    if (searchInput) {
        var sessionToken = getSessionToken();
        var string = searchInput + '&sessiontoken=' + sessionToken;
        $.ajax({
            url: 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' + string + '&key=AIzaSyCSL1_nsETBMG-E3_7Yk2N7wF5kZikNZZI',
            method: 'get',
            dataType: 'jsonp',
            beforeSend: function () {
                $b.addClass('is-searching');
            },
            complete: function (d) {
                $b.removeClass('is-searching');
                if (typeof d.responseJSON.predictions != 'undefined' && d.responseJSON.predictions.length) {
                    var data = d.responseJSON.predictions;
                    var html = '';
                    var firstId;
                    $.each(data, function (k, v) {
                        if (k < 6) {
                            var parts = v.description.split(', ');
                            var name = parts[0];
                            parts.shift();
                            var other = parts.join(', ');
                            var active = k == 0 ? ' active' : '';
                            firstId = v.place_id;
                            html = html + '<div class="suggestion' + active + '" data-place="' + v.place_id + '">' + ' <b>' + name + '</b>' + (other ? ', ' : '') + other + '</div>';
                        }
                    });
                    $('.suggestions > div').html(html);
                    if (html == '') {
                        $('.suggestions').removeClass('show');
                    } else {
                        if (setIfLoads && firstId && typeof getStarmapDetails != 'undefined') {
                            getStarmapDetails(firstId);
                        }
                        if (makePick) {
                            makePick = false;
                            $('.suggestions').removeClass('show');
                            $('.suggestion').eq(0).click();
                        } else {
                            $('.suggestions').addClass('show');
                        }
                    }
                    setIfLoads = false;
                }
            }
        });
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-05-10
@Rsa97

responseJSON is a $.ajax property, not the return data. Just print to the console what comes in success (console.log(d)) and see how to parse the result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question