What could be the reason my function is not showing any data?

Using AngularJS in web development

$scope.fetchData = function(dateTime) {
    console.log(dateTime + " fetch");
    $http({
        method: 'GET',
        url: 'https://api.example.com/appointments',
        params: {
            dateTime: dateTime
        },
        headers: {
            'X-API-Key': 'XXXXX',
            'X-API-REST-Key': 'XXXXX',
        }
    }).success(function(response, params, status, data, headers, config) {
        console.log();
        $scope.appointments = data;
    }).error(function(params, status) {
        console.log(dateTime + " error");
    });
};

Using HTML to display appointments

<ion-view title="Appointments" right-buttons="rightButtons">
    <ion-content has-header="true" has-tabs="true" padding="true">
        <button class="button button-light" ng-click="fetchData()"> button-light </button>
        <ul>
            <li ng-repeat="item in appointments"> {{ appointments.dateTime }} </li>
        </ul>
    </ion-content>
</ion-view>

Although the console shows that the function is successful, the data is not being displayed as a list.

The payload example for one of the appointments includes a 'time' field, but it is not being shown in the list.

{
    "results": [{
        "createdAt": "2015-12-30T15:03:48.511Z",
        "objectId": "BjP1zZ8JqD",
        "time": "12:24",
        "updatedAt": "2015-12-30T15:03:48.511Z"
    }]
}

Answer №1

Since you are not accessing the data correctly,

Initially, you are doing this:

$scope.appts = data;

This means to access the first time, you would need to first access results, then the array, then time

$scope.appts.results[0].time

What you probably really want is

$scope.appts = data.results;

Because results is actually the array of data

Secondly, you are using the incorrect angular expression

<li ng-repeat="item in appts"> {{ appts.time }} </li> </ul>

appts represents the entire array, not one of the objects in the array, which would be item

<li ng-repeat="item in appts"> {{ item.time }} </li> </ul>

Answer №2

It seems like in your response, you mentioned the following:

{"results":[{"createdAt":"2015-12-30T15:03:48.511Z","objectId":"BjP1zZ8JqD","time":"12:24","updatedAt":"2015-12-30T15:03:48.511Z"}]}

Additionally, in the HTML code, you are referencing appts.time, but it should actually be appts.results [0].time.

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

Error: res.send is not a valid method in Node.js

I am encountering an issue with the code below, specifically receiving an error stating "res.send is not a function". I would appreciate any assistance. Code Snippet: var http = require('http'); var fs = require('fs'); var connect = ...

Problem encountered while attempting to sort a table with JavaScript and jQuery

Having trouble sorting my table by the content in the first column. I've tried implementing code similar to the one found in this ANSWER, but unfortunately, it's not working as expected for me. When you run the snippet, you can see that the table ...

Combine the elements within the HEAD tag into a group

I am currently developing a dynamic page system using AJAX. Whenever a link is clicked, an AJAX call is made to retrieve data from the requested page. However, I have encountered an issue where certain data in the page file needs to be placed within the h ...

Harnessing the Power of JSON in Rails to Enhance Your jQuery UI Autocomplete Form

In the context of a rails application, I have been exploring different methods to fetch data from a JSON file. Despite my efforts, I have not been able to find a solution that suits my requirements. The static data I have is quite extensive, consisting of ...

Using Javascript within AEM to update a class upon checkbox selection

I need assistance targeting the 'horizontal-video' class within a div in an AEM component. I am attempting to add a second class called 'flipped' to the div if the author clicks on a checkbox with the ID of 'coral-id-540'. Unf ...

Module for managing optional arguments in Node.js applications

I'm on the hunt for a Node.js module that can effectively manage and assign optional arguments. Let's consider a function signature like this: function foo(desc, opts, cb, extra, writable) { "desc" and "cb" are mandatory, while everything else ...

Stop the click event using a confirmation dialog before proceeding with the operation

I'm trying to implement a confirmation dialog before deletion by using e.preventDefault() to prevent immediate deletion. However, I am facing an issue in the YES function where I would like to resume the click event's operation with return true w ...

Ways to combine two arrays using dual requests in JavaScript with Mailchimp

The issue with the title not being very clear has been noted. Allow me to elaborate on my problem. Below is the code snippet: mailchimpMarketing = require("@mailchimp/mailchimp_marketing"); mailchimpMarketing.setConfig({ apiKey: "MY API KEY", server: "MY ...

Showing data from a JavaScript controller's JSON response in an HTML table

I'm currently developing a spring app using AngularJS and I have a response coming from a JS controller. My goal is to showcase this response in a table on the webpage. The object devDebtTable is accessible to the page from the JS controller. The JS ...

Trouble with the $.inArray function

Encountering an issue with storing objects into an array. However, when attempting to verify the existence of an object using $.inArray, it consistently returns -1. The code is written in AngularJS. <input name="{{question.number}}" ng-clic ...

Utilizing Moment.js: Transforming 12-hour format to a Date object

Is there a way to convert a 12-hour string into a 24-hour Date object? day.from = day.from || moment("6:00", ["h:mm"]).format("HH:mm"); Unfortunately, I am encountering the following error: angular.js:11706 Error: [ngModel:datefmt] Expected `6:00` to be ...

Error in Redux reducer file caused by an incorrect data type in action.payload

I have encountered a type error in my reducers' file where it says that my 'Property 'address' does not exist on type 'number | { connection: boolean; address: string; }'. This issue is arising in my React application while us ...

Tips for uploading a file with fetch

While I know this question has been asked before, none of the solutions seem to be working for me. Initially, I attempted to resolve the issue using axios, but it appears that there is a bug preventing me from utilizing it for file uploads. Therefore, I am ...

The angular UI router is a powerful tool for navigating

Attempting a simple layout but nothing seems to render. The Layout.html file is being loaded, but that's all I see. No errors in the console either. Here is a snippet from my config file: $stateProvider .state('layout', { ...

How can I substitute a specific capture group instead of the entire match using a regular expression?

I'm struggling with the following code snippet: let x = "the *quick* brown fox"; let y = x.replace(/[^\\](\*)(.*)(\*)/g, "<strong>$2</strong>"); console.log(y); This piece of code replaces *quick* with <strong& ...

I am experiencing issues with the rendering of my q-btn elements in Quasar and they are

I recently started using Quasar and encountered an issue with my q-btn component buttons displaying white backgrounds at random, despite having added a background-color in the external stylesheets. Here are some examples of this perplexing problem: https ...

The method of iterating over a string in key-value pairs

How can I efficiently loop through a string and extract key/value pairs? The data is provided to me as a single string using the jstorage plugin. I attempted to split the string into an array, but the resulting key/values were not as expected. For exampl ...

Tips for transforming this jquery script into "vanilla" javascript

Looking for assistance in translating this jQuery code into vanilla JavaScript (without the need for the jQuery library). var myConsole={ panel:document.querySelector('#something'), log:function(message){ this.panel.innerHTML+= m ...

Maximizing efficiency in JavaScript by utilizing jQuery function chaining with deferred, the .done() function

I am working on retrieving data from multiple functions and want to chain them together so that the final function is only executed when all the data has been successfully loaded. My issue arises when trying to use the .done() method, as it calls the func ...

I've encountered a peculiar error that is new to me while using bootstrap and javascript. Can anyone shed light on what this error signifies?

Hey there! I've come across this error in the console while attempting to utilize Bootstrap. It seems that a style is being refused from 'http://127.0.0.1:5500/node_modules/font-awesome/css/font-awesome.min.css' due to its MIME type ('t ...