When retrieving items from an object store in HTML, the result displayed is often [object Object]

Currently, I am in the process of iterating through all objects within the object store named "tasksStore". However, when attempting to display these objects in my HTML, only [object Object] is returned. Even if I try to specify a specific part of the object to retrieve (e.g. getTasks.result.title), it still returns undefined.

I am puzzled as to what mistake I might be making. When I log the result, I receive the standard object information:

I initially thought that the data types for put and get were causing issues, but after investigating further, that does not seem to be the problem.

Javascript:

function listTask() {
    //establish connection to the database
    let request = window.indexedDB.open("KanbanDatabase", 2), 
    db,
    tx,
    store,
    index;

    //error handler for the connection
    request.onerror = function(e) {
        console.log("An error occurred while listing tasks: " + e.target.errorCode);
    }

    //success handler for the connection
    request.onsuccess = function(e) {
        console.log("Tasks listed successfully");
        db = request.result;

        //define transaction, store, and index
        tasksTx = db.transaction("tasksStore", "readwrite");
        tasksStore = tasksTx.objectStore("tasksStore");
        tasksIndex = tasksStore.index("title", "status");

        //error handler for the request result
        db.onerror = function(e) {
            console.log("ERROR " + e.target.errorCode);
        }

        //variable for counting objects in the index
        let amountOfTasks = tasksIndex.count();

        //error handler
        amountOfTasks.onerror = function() {
            console.log("An error occurred while determining the number of tasks")
        }

        //success handler
        amountOfTasks.onsuccess = function() {
            //console.log("Tasks: " + amountOfTasks.result);
            //TODO: add destination to the function to be able to list tasks with the specific statuses
            for (var i = 0; i < amountOfTasks.result+1; i++) {
                let getTasks = tasksStore.get(i);

                let getTasksElementContainer = document.getElementById("list-tasks");
                let createTasksList = document.createElement("li");
                createTasksList.id = "task-" + i;
                
                getTasks.onerror = function() {
                    console.log("An error occurred while looping through the tasks")
                }

                getTasks.onsuccess = function() {
                    
                    console.log(getTasks.result);
                    getTasksElementContainer.appendChild(createTasksList);
                    createTasksList.innerHTML = getTasks.result;
                    //createTasksList.innerHTML = getTasks.result.title - does not work, returns undefined

                }
            }   
        }
    }
}

Answer №1

To convert the object into a string, utilize the JSON.stringify method:

getTasksElementContainer.appendChild(JSON.stringify(createTasksList));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you use ES6 pure Javascript to set the src attribute of an html collection of images from an array of paths as values for each image?

Currently, I have an array filled with different src values like [path1, path2, path3], and a corresponding HTML collection which has the same number of elements as the array. I am looking for a way to assign each value from the array to set the src attrib ...

Tips for sorting on an ANTD table with a JSON array column instead of a string

I am facing an issue with filtering and searching in an antd table. The table has 2 columns, and I want to filter on the first column and search text on the second one. After rendering the application, everything seems fine. However, there is a problem re ...

I have a Key in my component, but it is still searching for a unique key

Just diving into React JS and attempting to transfer data from one component to another using the Link to method I found online... Error: index.js:1 Warning: Each child in a list should have a unique "key" prop. Checking the render method of Videos. Refe ...

Laravel - AutoComplete Feature: Populate Input Field with JSON Data Instead of Suggestions

I've been working on adding search functionality to my Laravel 5.4 project, but I'm running into an issue. The suggestions show up fine in the dropdown menu, but when I select one, the input field gets filled with JSON data instead of the suggest ...

Performing addition of two large numbers in C. Length of dynamically allocated array adjusts once the array is modified within a function

Currently, I am in the process of developing a program that can handle the addition of two very large numbers using arrays of type char. In order to allow for cases where the sum may be one digit longer than the longer input number, I have dynamically allo ...

Retrieving information from a nested .then callback

Summing it up briefly, I've been diving into the depths of learning JavaScript for a while now. After hours and hours of relentless Googling, I find myself stuck with an issue that eludes resolution, pointing to my flawed approach. The objective at h ...

What is the best approach for managing caching effectively?

My SPA application is built using Websocket, Polymer, ES6, and HTML5. It is hosted on a Jetty 9 backend bundled as a runnable JAR with all resources inside. I want to implement a feature where upon deploying a new version of the JAR, I can send a message ...

Utilize the Power of JQuery Range Slider to Dynamically Update CSS Property Values (specifically column / fr and column-gap)

Currently, I am in the process of creating a JQuery range slider that can dynamically update the columns and column-gaps of my photo grid. While searching for a solution, I came across a relevant post where someone had shared their code. However, there see ...

Pressing the button twice is necessary for troubleshooting when initially opened

After creating a code snippet that hides or shows part of the text when a button is clicked, I noticed that upon launching the page, it requires two clicks on the button to function properly. This issue occurs only during the initial launch, as after the f ...

Getting access to a JavaScript class from TypeScript and initializing it

I have integrated jsPDF by adding its jspdf.min.js file to my project. However, I am facing an issue when trying to use it within a TypeScript class. The error message states that jsPDF is unknown: btnPrintReportClick() { var pdf = new jsPDF(); ...

Adding a line break in a Buefy tooltip

I am trying to show a tooltip with multiple lines of text, but using \r\n or is not working. <b-tooltip label="Item 1 \r\n Item 2 \r\n Item 3" size="is-small" type="is-light" position="is-top" animated multilined> ...

Issue with AngularJS for loop incorrectly incrementing when there is a single item in the array

Within my Angular application, there is a snippet of code that looks like this: for (var i = 0; i < $scope.itemList.length; i++) { if ($scope.itemList[i].serialNumber == quickCode) { console.log(i) returnsService.getNoReceiptErrorMe ...

Calculating the frequency of specific substrings present in the complete object

I am seeking a method to tally substrings of object values. In other words, instead of the entire object containing a string, I want it where one key equals a string. An effective Xpath in XSLT is: count(//v[contains(.,current-grouping-key())]) However, ...

Utilizing a pointer to a multidimensional array from a different class and then implementing it

My goal is to access the 'room' array using functions within the room class itself. Here's a snippet of my current code: class ship{ room * rooms[3][4]; public: room ** getrooms(){ return *rooms; } } class room{ ...

Enlarging the modal overlay

My Angular/Bootstrap app features a small text area within a modal window that often contains lengthy content exceeding the size of the textarea and modal itself. I am looking to incorporate a button within the modal window that, when clicked, opens a lar ...

What is the best way to arrange an array of objects in JavaScript by numerical order and then alphabetically?

Possible Duplicate: Sorting objects in an array by a field value in JavaScript I'm looking to sort an array of objects both numerically (by id) and alphabetically (by name). However, the current method I'm using is not giving me the desired ...

Unable to establish SocketIO callback from client to server resulting in a null object instead

I'm encountering an unusual issue with SocketIO. On the server-side, I am using the emit() method like this: $s.sockets.emit(scope, {some: datas}, function(feedback) { console.log('received callback'); } ) ...

Implementing the Applet Param Tag using JavaScript or jQuery

<applet id="applet" code="myclass.class"> <param name="Access_ID" value="newaccessid"> <param name="App_ID" value="newappid"> <param name="Current_Url" value="newticketurl"> <param name="Bug_ID" value="newbugid"&g ...

ng-class not updating using a combination of object and string

I am just starting to learn angular js and I recently came across the ng-class directive. Below is an example of how I have used it: ng-class="[{'highlighter-row-Class' : (file.id == 1 && file.processed), 'bold-row-Class' : ...

Issue with jQuery Master-Detail dropdown list selection

Having trouble isolating the code in this jsfiddle script. Despite my efforts, it seems that isolation is not working as expected and Firebug isn't showing any errors on the console: <!DOCTYPE html> <html lang="en"> <head> ...