What causes JavaScript functions to return a NaN value?

While attempting to utilize an int value from a JSON file:

{
 "buenos":274.0,
 "desaprobacion":0.13564668769716087,
 "malos":43.0,
 "proporcion":6.372093023255814
}

within the app:

var app = angular.module('angularSpa', ['ngRoute','nvd3'])

    .service('QueryService', function($http){
        var urlBase = 'http://localhost:8080/tweets';  
        this.getJson = function(){
            return $http.get(urlBase+"/compañias/vtr");
        };  //This code has been successfully used in another controller to display values within an html
        //... more functions

})

in the controller:

   function getValue(){
        QueryService.getJson()
        .success(function(data){
            return JSON.parse(data.buenos); // always returns NaN here
        })
        .error(function(error){
            $scope.status = 'Error at query';
            console.log('error');
        });
    }

Could there be an issue with the return of getValue()?

Answer №1

Firstly, you are parsing data.buenos, but the JSON is actually in the data itself (JSON.parse(data.buenos);).

It would be more appropriate to parse the entire data first and then return the parsed data buenos.

 var parsedData = JSON.parse(data);
 return parsedData.buenos;

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

Employ variables as a jQuery selector

let myLink = "#portfolio-link-" + data[i].pf_id; I am storing an ID in a variable. $("#pf-container-1").append(portfolio); console.log(myLink); $(myLink).append(function(){ $("<button class='btn btn-inverse' id='portfo ...

Unveiling the method to fetch the emitted value from a child component and incorporate it into the parent component's return data in Vue

How can I retrieve the title value passed by the Topbar component and incorporate it into the data() return section? I attempted to create a method to pass the value, but was unsuccessful despite being able to successfully log the value in the parent file. ...

Angular Typescript filter function returning an empty arrayIn an Angular project, a

I have a filtering function in Angular that is returning an empty array. Despite trying similar solutions from previous questions, the issue persists. The function itself appears to be correct. Any assistance would be greatly appreciated. gifts represents ...

The database is not displaying any information on the webpage

Upon loading my home.php page, the intention is to display the data from the 'blog_post' table in the database. However, despite the record being inserted correctly, no data is being shown on the page. I have searched for solutions to this issue ...

"The website seems to be experiencing some technical difficulties on Firefox, but I have switched to

I attempted to reset the text area after sending a message with the code below: $(document).keypress(function (e) { if (e.which == 13) { e.preventDefault(); var $form = $('#f1'); $.ajax({ url: $form.attr( ...

issue with the submit button due to non-functional base64 encoded image

After successfully converting multiple file images to base64 format, I encountered an issue when trying to submit the values. When a user selects multiple images and clicks the submit button, the converted base64 values are returned as undefined. How can t ...

Receiving a 2032 IOError when making an API request using custom headers in Flash AS3: POST method shows as GET

I'm currently working with a file that is making an API request to retrieve JSON data. This request requires 2 custom headers, and from what I understand, custom headers necessitate the request to be a POST. After going through various questions on d ...

Sending values to Vuex getters from a Vuex action

I have been utilizing a Vuex getter across various components in my application. However, I recently encountered a scenario where I require slightly more intricate logic before accessing the getter, leading me to employ a Vuex Action. The challenge now is ...

Issues encountered when adding information to a table using Access VBA

I am facing an issue with my Access database setup. I have a VBA module that generates a JSON string stored in a variable called JsonStr2. My goal is to insert this JSON string into a table named JSONforAPI which has two fields: ID = Auto number JSON = L ...

Seeking assistance in comprehending the method for styling table data using inline CSS in a JavaScript file

I am currently using a JavaScript file that was created for me to update form data based on user selections and display radio buttons along with the corresponding costs. Although the inline CSS in this script works perfectly fine in Chrome and Firefox, it ...

Implementing a blur effect on a CSS loader

I have successfully implemented a loader feature where a loading animation is displayed upon clicking the submit button, indicating that the form submission is in progress. However, I am encountering an issue when trying to apply a blur effect to the entir ...

Modifying a JArray within a JObject using JSON.NET

I have two files, one in JSON and the other in XML. I need to merge both of them, so I decided to convert the XML into JSON and then concatenate/merge them. { "Level1": { "Level2": [ { "id": "Chart", ...

When attempting to incorporate JSX or markup within React.js, the closing tags are mysteriously transformed into unreachable code

I am currently learning react-js and I have encountered a problem with my code. Whenever I try to add markup or JSX in the designated area 'write code', my text editor displays syntax errors, disables closing div tags, and even throws unreachable ...

Assigning active classes based on the href attributes of child <a> tags

I've created a navigation structure as follows: <ul class="page-sidebar-menu"> <li class="menu"> <a href=""> <span class="title">Menu Item</span> <span class="arrow"></span> < ...

What is the best way to activate a code when the user interacts with the app icon?

Remote Notifications are designed to only be activated when the user clicks on one of them by default. However, even if the red "badge" is displayed after clicking on the app icon, notifications are not considered, as stated in the documentation: When ...

My controller in AngularJS is having trouble fetching the latest values of the $scope variables

In my code, I've included the relevant snippet below. The base angularJS seems to be functioning properly as the HTML document doesn't display {{}} variables but instead remains blank. I suspect that this is due to the variables receiving a null ...

Tips on altering the standard message requesting an email address with an @ symbol included

I want to modify the error message displayed when an invalid email is entered, but I am unable to access the source code. I believe the source code triggers setCustomValidity based on the appearance of the error message. Here is the relevant element: < ...

Expanding the capabilities of angularJS modules

I have a background in Java and am currently learning Angular JS. I recently wrote my first module and now I want to extend it. To me, it seems like a Class that can be extended. So, I created the following code with two modules: myApp and myApp2. Angular ...

Why do I encounter the issue of receiving a "function not defined" error when using setInterval in conjunction with jQuery?

I need to make updates to a user's field using jQuery. Here is my code snippet... jQuery(document).ready(function(){ setInterval("keepUserActive()", 6000); function keepUserActive() { jQuery.post('/users/update_useractive', ...

What is the best way to extract a specific section of JSON data using nodejs?

I am currently working on a nodeJS script that listens for webhook events and then adds the data to firebase. The script is functioning well with the following code: let data = { addresses: event.data["addresses"], c ...