TypeError: b is undefined jquery.min.js: 3: 6371

0

I'm trying to create a function that, given the type of post, returns the findings that are in the database using jQuery-min-3.1.1.js , PHP, and JSON to exchange information. However I get the following error message: TypeError: b is undefined jquery.min.js:3:6371 I did not declare this var in my script, and I do not understand why the error occurred.

jquery / javascript:

function loadPost() {
    var size = arrayPost.length;
    var imgPath = '/images/upload/poster/';
    var post = arrayPost;
    for (i = 0; i < post.rows; i++) {
        $('.side-left').append(htmlPost);
        $($('.cont-desc')[i]).attr('title', post.description[i]);
        $($('.cont-type')[i]).html(post.type[i]);
        $($('.cont-url')[i]).attr('href', post.url[i]);
        $($('.cont-url-img')[i]).attr('src', imgPath + post.poster[i]);
        $($('.cont-demo')[i]).html(post.title[i]);
    }
}
//navbar---------------------------------
var bcd = 0;
var ecd = 12;
function mountNewPubli(d) {
    $('.side-left').html('');
    arrayPost = d;
    loadPost();
    $('.side-left').append('<button class="btnloadmore" type="button">Carregar +</button>');
}
$('.search-label').click(function (ev) {
    ev.preventDefault();
    var label = $(this).attr('data-type');
    $.ajax({
        type: 'GET',
        url: '/sys/etc/search-label.php',
        data: {type: label, b: bcd, e: ecd}
    }).done(function (d) {
        console.debug(d)
        if (d.status == 404) {
            callError(404, 'Não foi possível obter ' + $(this).html() + '{search-label: return 404}');
        } else if (d.status == 200) {
            bcd = (ecd + 1);
            ecd += 12;
            mountNewPubli();
        } else {
            console.debug(d);
        }
    }).fail(function () {
        callError(404, 'Não foi possível obter ' + $(this).html() + ' {Path_not_found}');
    });
});

PHP:

<?php

include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/lib.php';

if (isAjax()) {
    if (isset($_GET['type'])) {
        include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/class/class-posts.php';
        $post = new Publication();
        $return = $post->searchLabel($_GET['type'], (int) $_GET['b'], (int) $_GET['e']);
    }
    header('Content-Type: application/json');
    echo json_encode($return);
}
    
asked by anonymous 03.02.2017 / 21:48

2 answers

1

First of all let's say the loadPost function:

function loadPost() {

    var size = arrayPost.length;
    var imgPath = '/images/upload/poster/';
    var post = arrayPost;

    ...

}

Here you retrieve the value of arrayPost , both to assign the value of size and post , but the arrayPost variable is not defined in this scope, only within the mountNewPubli function, where function loadPost is called. Then try passing the value of this variable per parameter:

function loadPost(arrayPost) {

    var size = arrayPost.length;
    var imgPath = '/images/upload/poster/';
    var post = arrayPost;

    ...

}

Now the function mountNewPubli . In it, we just need to pass the parameter we just defined:

var bcd = 0;
var ecd = 12;

function mountNewPubli(d) {

    $('.side-left').html('');
    arrayPost = d;
    loadPost(arrayPost);

    ...

}

However, we must remember that the mountNewPubli function has a d parameter, which does not have its value passed during the call, in the callback of the AJAX request.

$('.search-label').click(function (ev) {

    ev.preventDefault();

    var label = $(this).attr('data-type');

    $.ajax({
        type: 'GET',
        url: '/sys/etc/search-label.php',
        data: {type: label, b: bcd, e: ecd}
    }).done(function (d) {

        console.debug(d)

        if (d.status == 404) {
            ...
        } else if (d.status == 200) {

            bcd = (ecd + 1);
            ecd += 12;
            mountNewPubli(); // <---- AQUI

        } else {
            console.debug(d);
        }

    }).fail(function () {
        ...
    });
});
Since this d is the same as the callback function, we pass its value to the function mountNewPubli :

$('.search-label').click(function (ev) {

    ev.preventDefault();

    var label = $(this).attr('data-type');

    $.ajax({
        type: 'GET',
        url: '/sys/etc/search-label.php',
        data: {type: label, b: bcd, e: ecd}
    }).done(function (d) {

        console.debug(d)

        if (d.status == 404) {
            ...
        } else if (d.status == 200) {

            bcd = (ecd + 1);
            ecd += 12;
            mountNewPubli(d); // <---- AQUI

        } else {
            console.debug(d);
        }

    }).fail(function () {
        ...
    });
});
  

I believe that the use of the variables bcd and ecd is ok, since they appear to be defined in the global scope of the program.

Addendum # 1

As noted by the user André Luis Marmo , in this code snippet, the i index is in the wrong position.

function loadPost() {
    var size = arrayPost.length;
    var imgPath = '/images/upload/poster/';
    var post = arrayPost;
    for (i = 0; i < post.rows; i++) {
        $('.side-left').append(htmlPost);
        $($('.cont-desc')[i]).attr('title', post.description[i]);
        $($('.cont-type')[i]).html(post.type[i]);
        $($('.cont-url')[i]).attr('href', post.url[i]);
        $($('.cont-url-img')[i]).attr('src', imgPath + post.poster[i]);
        $($('.cont-demo')[i]).html(post.title[i]);
    }
}

The correct one would be:

function loadPost() {
    var size = arrayPost.length;
    var imgPath = '/images/upload/poster/';
    var post = arrayPost;
    for (i = 0; i < post.rows; i++) {
        $('.side-left').append(htmlPost);
        $($('.cont-desc')[i]).attr('title', post[i].description);
        $($('.cont-type')[i]).html(post[i].type);
        $($('.cont-url')[i]).attr('href', post[i].url);
        $($('.cont-url-img')[i]).attr('src', imgPath + post[i].poster);
        $($('.cont-demo')[i]).html(post[i].title);
    }
}

Addendum # 1.1

Since, as clarified in the comments, post , in the loadPost function, is not a list of objects, but an object in which each property is a list, addendum # 1 can be ignored for this solution.

    
03.02.2017 / 22:48
0

You do not have a "b" variable, but you are passing an object that contains an "b" attribute. Maybe it could be something with this object being passed.

data: {type: label, **b**: bcd, e: ecd}
    
03.02.2017 / 21:58