Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: [object Object] on Javascript

I keep getting an error when I run the javascript below in Firebug. I've tried changing multiple things and it still outputs the error. I am working with an api to retrieve information from the XML and then output it onto the screen but I keep getting an object error. Can someone see why?? Any help is appreciated!

$(document).ready(function() {

    $('#searchbtn').bind('click' || 'enter',function(e) {
        if ($.trim($('#searchBox').val()) !== '') {
            $('#videos').append('<img src="img/loading.gif" alt="loading" class="loading" />');
            getVideos(e);
        }
    });
});

function getVideos(e) {
    e.preventDefault();
    var text = 'text='+$('#searchBox').val();
    $.ajax({
        url: 'getVideos.php',
        dataType: 'xml',
        type: 'POST',
        data: text,
        success: function(data) {                               
            $('#videos').append("<h1>The following events match your search!</h1>");

            var xmlString = data;

            if ($(xmlString).find('feed').children('entry').length == 0) {
                $('#videos').append('<p class="noResults">Sorry, no results for you! Try searching again!</p>');
            } else {
                var videoTitle = [];

                $(xmlString).find('title').each(function() 
                { 
                videoTitle.push($(this).text()) });

                $('#videos').append('<ul>');

                $(xmlString).find('entry').each(function(i) {
                    if (i == '40') {
                        return(false);
                    }
                    var vidInfo = '';
                    vidInfo += "<p>"+videoTitle[i]+"</p>";
                    $('#videos ul').append('<li>'+vidInfo+'</li>');
                });
            }
        },
        error: function(data) {
                console.log('Error: ' + data);
        }

    })
};
like image 424
LE12 Avatar asked Apr 14 '12 18:04

LE12


People also ask

How do you fix an object object?

To fix this, you can use the JSON. stringify() method to change the object into a string that can be popped up in the browser using the alert() method.

How do you check if an object is an error object?

To check if a variable is of type Error , use the instanceof operator - err instanceof Error . The instanceof operator returns true if the prototype property of a constructor appears in the prototype chain of the object. Copied! In this example we used the Error constructor to create a new error object.

How many types of error objects are there in JavaScript?

There are 7 types of JavaScript errors: Syntax error, Reference Error, Type Error, Evaluation Error, RangeError, URI Error and Internal Error. Errors in Javascript can be handled using the try... catch... finally construct as well as the throw operator.

What is object object in JavaScript?

The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.


1 Answers

When you append an object to a string, it gets its toString method called, which for a plain object just gives the infamous "[object Object]". To log an object, you should just pass it straight into the console.log function as an argument, like so:

console.log('Error:', data);
like image 53
Chuck Avatar answered Oct 08 '22 13:10

Chuck