The combination of Angular.js and a WebApi that generates an integer array as output

When it comes to testing with Angular, I have a WebAPI method that returns an object containing a List. The JSON output for this object is shown below.

In my Angular controller, I am trying to retrieve this object but seem to be encountering some issues.

Does anyone have any suggestions on what I might be doing wrong?

Using RestResource as a factory:

myngApp.factory("valueResource", function ($resource) {

    var result =
        {
            database: $resource("/api/values")
        }

    return result;
});

Controller:

myngApp.controller("dataController", function ($scope, settings, valueResource) {
    $scope.message = "Hello World!";
    $scope.userName = settings.user;


    $scope.restValues = valueResource.database.query({});
});

The JSON response (captured using Fiddler) looks like this:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpca2lsblxleHBlcmltZW50YWxcc3JjXG5nMlxXZWJBcHBsaWNhdGlvbjFcV2ViQXBpXHZhbHVlcw==?=
X-Powered-By: ASP.NET
Date: Mon, 17 Feb 2014 14:47:35 GMT
Content-Length: 19

{"Items":[1,2,3,4]}

Console output shows the following error messages:

XHR finished loading: "http://localhost:55671/api/values". angular.js:8013
Error: [$resource:badcfg] http://errors.angularjs.org/1.2.12/$resource/badcfg?p0=array&p1=object
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:6:450
    at p.then.m.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular-resource.min.js:8:517)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:94:173
    at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:102:456)
    at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:100:218)
    at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:103:264)
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:67:120) angular.js:9435

Answer №1

Upon analyzing the error message and consulting its documentation

http://docs.angularjs.org/error/$resource/badcfg

it becomes evident that the query function is expected to return an array, while your implementation returns an object with a single property named Items.

To resolve this issue, utilize the get method provided by the resource.

Answer №2

According to the guidance provided in the $resource documentation, it is recommended to adopt a different approach when creating models. In cases where a simple XHR request to a specific URL suffices, utilizing the $http service and handling the promise resolution directly within the view would be more appropriate.

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

Guide to validating a Chinese mobile phone number using regular expressions

I need help validating a Chinese phone number that must begin with the number 1 followed by 10 additional numbers, for example: 14375847232 Any assistance would be appreciated. Thank you. ...

Adjusting the size of MaterialUI SVG icon: a simple guide

Hey everyone, I'm having some trouble trying to make this MaterialUI icon larger. I've attempted changes with the viewbox and inline styling, but no luck so far. The current size of the icon isn't cutting it for me, I need it to be bigger. ...

Guide on Crafting an Interactive Breadcrumbs Component

How can I implement product category breadcrumbs on the product page? These breadcrumbs will represent the parent category of the product. I am utilizing Next.js and Strapi for this project. For example, here is a screenshot showing how it should look: ...

Having difficulty retrieving the initial selection value

Despite receiving all other values, I am unable to retrieve the event.everyday value. Can you please help me identify what I might be doing incorrectly? Situation - Choose Every day from the dropdown menu. Click on the "click it" button and review the ...

What could be causing the issue of the function not rendering in the React app

Even though the code runs without errors in the console, the React app is not displaying any JSX content and just shows a blank page after adding an if block. Interestingly, removing the if block makes the code work as expected. import { useState } from ...

What is the reason behind the cross-origin error thrown by JSON.parse?

When I don't use JSON.parse, my code works perfectly fine. However, as soon as I attempt to parse or stringify my data object, a cross-origin error is thrown. Why is this occurring and what steps can I take to resolve it? The snippet of code in Title ...

Is there a way to convert my function into a variable in order to execute an array of statements

I'm struggling to convert this code into a variable. I need to bind it inside a statement execute array. This is the code I am working on, which retrieves the current date and timezone. I attempted using $date = function() {}, echo $date(); but that ...

Having difficulty importing a .glb file in Three.js with the import.meta.url method when using the parcel bundler

Greetings! I recently embarked on creating a fun little game using Three.js for me and my friends. After watching numerous tutorials on how to import .gltf and .glb files, I decided to use the parcel bundler with the command: npx parcel ./src/index.html to ...

URL not functioning properly on Django CMS menu

After setting up django-cms and creating a collapsible menu with categories and subcategories, I encountered an issue. When clicking on a main category, the URL appears correct but it does not navigate to the corresponding page. Main categories without chi ...

Angular cookies may expire, but using the back button always revives them

Utilizing angular's cookie library, I have successfully set a cookie to store the id and passcode of a shopping cart on the backend. However, despite setting the expiration date to a past time in order to expire the cookie once the cart is purchased, ...

React: When an array state is controlling my components, why aren't they re-rendering?

I am facing an issue with my app where the className of buttons is not updating correctly when clicked. It seems that only active buttons trigger a re-render, while non-active ones do not. This behavior is confusing to me. Here's the code snippet for ...

Using jQuery to Retrieve an Element's Class Through an AJAX Call

For my website, I am using ajax to load pages by replacing the content in the main tag. The issue arises when working with Wordpress, as each page has its own set of body classes for styling purposes. My goal is to replace the old page's body classes ...

Button click does not display Div

As a newcomer to programming, I apologize if my question seems trivial or if the code I provide is not flawless. My goal is to develop a basic web application in Flask that enables users to monitor their CO2 emissions by entering data about their daily con ...

Ways to capture information from HTML with the help of JQuery

In the code snippet below, I am attempting to extract values from the h3 element and the data within the p elements. My goal is to have the extracted content in a format similar to this: NYC Row Restaurant Week From April 20st through April 28th Mother&a ...

Tips for interpreting the JSON data returned by a Node server in Angular

When trying to implement a login form in my Angular frontend, I encountered an issue with reading the JSON object returned from my node.js server. Despite following the correct steps, the console displays "undefined" as if it cannot recognize the format. ...

execute action after a specific point in time in the video is reached

How can I hide a div after a video has been playing for a certain amount of time? The code provided below seems like it should work in theory, but is currently not doing so. Any suggestions on how to fix this issue would be greatly appreciated! <body ...

What is the most optimal location to retrieve data for authorized users in a Vue application?

Greetings! I am currently retrieving an array of posts from my express API in the Home.vue file, which is secured by route guards. <script> export default { created() { this.$store.dispatch('fetchPosts') } } </script> The fet ...

Comparing Redux and MVC models in software architecture

A new project is on the horizon, and the Product Owner has suggested using Redux for state management. However, I am hesitant to embrace this suggestion as I fail to see the advantages compared to a model-based approach. For instance, instead of utilizin ...

Does a document.onmodification event exist, or something similar?

Is there a specific event in JavaScript that triggers whenever an element is added, removed, or modified? Although lacking in detail, it is a straightforward question. ...

Setting a function key, such as F2, to quickly rename a tab is a convenient way to

Looking at the screenshot provided, it is clear that renaming a selected tab requires clicking on it, while deleting an unselected tab necessitates a mouse hover. Unfortunately, these actions are not accessible via keyboard shortcuts. To address this issue ...