Typeerror: b is Undefined jquery.min.js:3:6371

Asked

Viewed 300 times

0

I’m trying to create a function that given the type of posting returns the findings that are in the database using jQuery-min-3.1.1.js, PHP and JSON for information exchange. But I get the following error message: TypeError: b is undefined jquery.min.js:3:6371 I didn’t declare this var in my script, and I don’t understand why it was wrong.

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);
}
  • Apparently it is showing error inside the jQuery file. Are you sure you have closed all HTML tags correctly? If possible, post the code here.

  • Yes they are all closed.

  • Put the rest of the code, maybe there’s no mistake.

2 answers

1


Let’s take parts, first function loadPost:

function loadPost() {

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

    ...

}

Here you recover the value of arrayPost, both to assign the value of size how much to post, but the variable arrayPost is not defined in this scope, only within the function mountNewPubli, where the function loadPost is called. Then try to pass the value of this variable by 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 function mountNewPubli has a parameter d, that 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 () {
        ...
    });
});

whereas this d is the same as the function callback, 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 variables bcd and ecd is ok, since they seem to be defined in the overall scope of the program.

Addendum #1

As noted by the user André Luis Marmo, in this code snippet, the index i 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 right thing 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

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

  • Wow old helped and a lot! The variable arrayPost is declared yes at the beginning of the script. I did not post it completely because it was extended so I tried only the parts that are used in this action and ended up forgetting the variable. After I passed the ajax result by parameter to mountNewPubli() like you said, the algorithm worked.

  • 1

    In the loop of loadPost() the [i] passed at the end of the object function attribute. Is that instead of creating an object for each post I put them as an array inside the attributes and then call them.

  • It makes sense. This is the problem of not passing the code completely. It leaves it ambiguous. I inserted in the answer the addendum #1.1 clarifying this part.

0

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

data: {type: label, **b**: bcd, e: ecd}
  • I’ve already changed that "b" but it’s still the same mistake.

  • Is this excerpt "post.Description[i]" correct? It wouldn’t be post[i]. Description

  • @Andréluismarmo, well observed. I added to my reply an addendum quoting its correction.

  • I commented in the @Andersoncarloswoss reply why.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.