When using `console.log`, the object is displayed correctly. However, an error occurs when

Here is the code I've been working on:

function parseJSONData(jsonData){
    var property, type, name, identifier, comment, content; 
        
    for(property in jsonData){
        switch(property){
            case "type": type = jsonData[property];
            case "name" : name = jsonData[property]; 
            case "comment" : comment = jsonData[property];
            case "identifier" : identifier = jsonData[property];
            case "content" : content = jsonData[property];
        }
    }


    if(content.hasOwnProperty){
        console.log(content);
        parseJSONData(content);
    }

The output I am getting is as follows:

[Object] footer.js:59

TypeError: 'undefined' is not an object (evaluating 'content.hasOwnProperty') footer.js:58

I find this confusing because when I log the content, it shows an object and works fine. The error only appears when I try to recursively call the function. I have not added a return statement yet due to this issue, do you think I should?

Answer №1

It seems that you are using the conditional expression if(theContent.hasOwnProperty) to check if theContent is defined or not. The variable is declared at the top of the function and is only defined in the final case.

A more reliable way to determine if a variable is defined is shown below:

if (typeof theContent !== 'undefined`) { ... }

When the final case statement is not executed, theContent remains undefined. Trying to access hasOwnProperty on an undefined value will lead to the error you've encountered.

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

JavaScript Regular Expression Assistance Domain Snatcher

I am in the process of developing a JavaScript Regex and I am struggling to find the right pattern to match a specific domain. Let me provide an example for better understanding. I need a regex that can identify any domain that exclusively contains (text. ...

Leverage JSON files for pagination in NextJS

I am currently developing a science website where the post URLs are stored in a static JSON file. ScienceTopics.json- [ { "Subject": "Mathematics", "chapters": "mathematics", "contentList": [ ...

The error message states that the provided callback is not a valid function -

I seem to be encountering an issue with the code snippet below, which is part of a wrapper for the Pipl api: The main function here performs a get request and then retrieves information from the API Any assistance in resolving this error would be greatly ...

What is the best way to initiate the registration page through the @auth0/auth0-react library?

I've hit a roadblock in trying to automatically launch the sign-up (registration) page using @auth0/auth0-react. Previously, I would send mode which worked with auth0-js. So far, I have attempted the following without success: const { loginWithRedir ...

Can one track the moment when a user clicks on an advertisement within a YouTube video?

Can we track when a user clicks on an advertisement within a YouTube video? We have integrated the YouTube API v2 into our website, allowing users to watch videos. The issue is that when a user clicks on an advertisement, the YouTube video pauses automat ...

I'm encountering an issue in my node application where it is unable to access the push

I am a beginner in the world of node.js and express. Whenever I try to start my application using the command npm start, I encounter an error message saying Cannot Read property push of undefined from my index.js file. The problematic code snippet looks l ...

In Certain Circumstances, Redirects Are Applicable

I have set up Private Routing in my project. With this configuration, if there is a token stored in the localStorage, users can access private routes. If not, they will be redirected to the /404 page: const token = localStorage.getItem('token'); ...

Vue.js Applications tend to encounter Expected Errors originating from JavaScript

I've been diving into learning Vue.js using Visual Studio 2017. Currently, my goal is to create applications with multiple buttons that trigger the display of a message upon being clicked. However, when I attempt to run this code, I encounter the foll ...

What is the best way to make a vertical line using the CSS code provided in the link?

Check out this cool horizontal line design I found on stackoverflow: Click here for CSS code to create the line I really like the look of the line. Here's how I implemented it on my website: hr.fancy-line { border: 0; height: 5px; } hr ...

Utilizing jQuery to efficiently chunk JSON data through AJAX calls

Is there a way to efficiently handle the retrieval of large amounts of JSON data through an ajax request? I am looking for a jQuery or JavaScript function that can assist with "chunking" the data. For example, I would like the ability to continuously rece ...

In React JS, the material-ui Accordion component's onchange function is failing to retrieve the current value

I am encountering an issue with the @material-ui Accordion where the onChange function is not capturing the current value. For example, when I click on the panel1 icon, it opens panel2 instead of taking the current value on icon click. I have provided the ...

Cannot execute the function test()

I'm just diving into the world of JavaScript and have put together a little fiddle with my code. Check it out here. However, I've run into an issue where removing CDATA makes it work fine in fiddle but causes problems in XHTML editors like Eclip ...

Tips for choosing the injected HTML's List Element (li) while ensuring it remains concealed

How do I hide the list item with iconsrc="/_layouts/15/images/delitem.gif" image when this div is injected by a first party and I am a third party trying to conceal it? $("<style> what should be inserted here ???? { display: none; } </style>") ...

XMLHTTPRequest is experiencing issues with displaying the progress bar

I'm facing an issue with uploading images in PHP while showing the progress status. The image uploads correctly using XMLHttpRequest, but I can't see the progress bar moving. Below is my code. Can someone help me solve this problem? <html> ...

How to Use AJAX, jQuery, and JSON to Send an Array to PHP

I'm attempting to send an associative array through AJAX $.post to a PHP script. Below is the code I am using: var request = { action: "add", requestor: req_id, ... } var reqDetails = $("#request_details").val(); ...

How can I add an object to an array of objects in Vue.js?

Hey there! I'm new to Vue and currently working on understanding the whole concept of Vue and how to use it. Right now, my focus is on learning lists. JS: Vue.config.productionTip = false; new Vue({ el: '#app', data: { items: [ ...

What is the most efficient way to transform HTML into React components effortlessly?

I have been attempting to automate the process of converting HTML into React components. I followed the link below to automatically convert HTML into React components: https://www.npmjs.com/package/html-to-react-components However, I encountered an issue ...

How can I display a Skeleton when pressing pagination buttons and then turn it off after 3 seconds?

Here is a snippet of my code featuring the use of a Skeleton as a placeholder for CardMedia: {loading ? ( <Skeleton variant="rectangular" width={308} height={420} animation="wave" /> ) : ( <CardMedia s ...

Preventing document.getElementById from throwing errors when the element is null

Is there a way to handle null values returned by document.getElementById in JavaScript without adding if statements or checks to the code? I'm looking for a solution that allows the execution of subsequent JavaScript calls even after encountering a nu ...

An error is occurring in asp.net where the ajax call is returning an undefined object

Here is the C# getter method that retrieves meanings of a word input by a user through a query. The meanings are then filled in a data table and added to a list of strings using a loop. Finally, the list is converted into a JSON string and returned: publ ...