Restangular fails to return the objects received from the REST API

Seeking assistance for a service issue. I have a service that can be accessed via the domain/service/webapi/ URL. When calling

domain/service/webapi/menu/getmenucategory
, it returns the JSON string representing the application's menu structure:

The problem arises when I make this call using Restangular, as I receive an empty object in return. Despite Fiddler confirming that Restangular is making the call to the web API and receiving the JSON response, I am puzzled why it does not return the expected Javascript objects.

I have reviewed the documentation but couldn't find a solution. It seems like I might have overlooked something simple.

Below are the snippets of code:

Controller:

'use strict';

dilibApp.controller('NavigationMenuController', function ($scope, navigationMenuService) {

    $scope.menuCategories = navigationMenuService.GetMenuCategories();

});

Service:

'use strict';

dilibApp.factory('navigationMenuService', function(Restangular) {

    return {
        GetMenuCategories: function() {
            //var menuCategories = Restangular.all('menu', 'getmenucategory');
            var menuCategories = Restangular.all('menu/getmenucategory');
            console.log(JSON.stringify(menuCategories.getList()));
            return menuCategories.getList();
        }
    }
});

Result in Chrome Console:

{"restangularCollection":true,"$object":[]}

JSON string:

[{"$id":"1",
"Id":1,
"Name":"File",
"SortNo":0,
"IsEnabled":1,
"MenuItems":[{"$id":"2",
        "Id":1,
        "Name":"File menu item 1",
        "IsEnabled":1},
        {"$id":"3",
        "Id":2,
        "Name":"File menu item 2",
        "IsEnabled":1}]},
{"$id":"4",
"Id":2,
"Name":"Administration",
"SortNo":1,
"IsEnabled":1,
"MenuItems":[{"$id":"5",
        "Id":4,
        "Name":"Administration menu item 1",
        "IsEnabled":1},
        {"$id":"6",
        "Id":5,
        "Name":"Administration menu item 2",
        "IsEnabled":1}]},
{"$id":"7",
"Id":3,
"Name":"Modules",
"SortNo":2,
"IsEnabled":1,
"MenuItems":[{"$id":"8",
        "Id":6,
        "Name":"Modules menu item 1",
        "IsEnabled":1},
        {"$id":"9",
        "Id":7,
        "Name":"Modules menu item 2",
        "IsEnabled":1}]}]

Answer №1

It appears that the issue lies in the way RestAngular handles data retrieval, as it returns a promise. In order to properly handle this, you will need to implement something along these lines:

$scope.menuCategories = [];
navigationMenuService.GetMenuCategories().then(function(result){
     $scope.menuCategories = result;
});

The problem arises when directly assigning the result, as you are only getting the promise object initially. This results in an empty array until the asynchronous operation completes and populates the array with the desired data.

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

Execute a JavaScript function when an element loaded via Ajax in a Spring MVC framework triggers the onChange event

I currently have a dropdown list with two values and I am looking to enable or disable four components based on the user's selection. If the user picks the first value, the components should be enabled, otherwise they should be disabled. On the main ...

The scope of a JS array is being lost in Firebase

The Firebase data structure looks like this: -users --demo1 ---conid:1 -election --election1 ---conRegex:1 --election2 ---conRegex:1 Here is the code to retrieve election1 and election2: var conid; var conRegex; var electionArr = []; if(uidA ...

Can one validate a single route parameter on its own?

Imagine a scenario where the route is structured as follows: companies/{companyId}/departments/{departmentId}/employees How can we validate each of the resource ids (companyId, departmentId) separately? I attempted the following approach, but unfortunate ...

How can you leverage Symfony to iterate through a JSON array efficiently?

After selecting a user, I am attempting to display a list of contracts. To achieve this, I have written the following query: /** * @param $firstname * @param $lastname * @return mixed * @throws DBALException */ public function getListPerUser($firs ...

Customized dropdown date filters for DataTables

Want to check out my test file? You can find it here: I'm currently working on a new dropdown feature labeled 'Sort By Year' that will extract all the dates from the 'Date' field in the JSON file. The goal is for the Sort By Year ...

Pressing the "Enter" key will submit the contents of

Hello, I have recently created a new chat box and everything seems to be working fine. However, I am facing an issue with submitting a message when I press enter (to go to the function Kucaj()). Can anyone provide assistance with this problem? I tried ad ...

Retrieving users by their Id's from MySql database using NodeJS

Goal: I aim to gather a list of users from a table based on the currently logged-in user. I have successfully stored all user IDs in an array and now wish to query those users to display a new list on the front end. Progress Made: I have imported necessa ...

Attempting to create a JavaScript class within an HTML document

Having an issue with instantiating a JavaScript class in a separate HTML file. The JavaScript class looks like this: class Puzzle { constructor(fenStart, pgnEnd) { this.fenStart = fenStart; this.pgnEnd = pgnEnd; } } module.exports = Puzzle; ...

Creating a validation error in Angular: When a user submits an empty form, radio buttons will be highlighted in red

I have encountered an issue already posted on GitHub regarding this matter, but unfortunately, no solution has been provided yet. You can view the issue here: https://github.com/angular/components/issues/11333 I was wondering if there are any workarounds ...

Binding textarea data in Angular is a powerful feature that allows

I am looking to display the content from a textarea on the page in real time, but I am struggling to get the line breaks to show up. Here is my current code snippet: app.component.html <div class="ui center aligned grid">{{Form.value.address}}< ...

Why won't JavaScript functions within the same file recognize each other?

So I have multiple functions defined in scriptA.js: exports.performAction = async (a, b, c) => { return Promise.all(a.map(item => performAnotherAction(item, b, c) ) ) } exports.performAnotherAction = async (item, b, c) => { console.log(`$ ...

Converting JSON data into XML format

When I am converting JSON Values to XML, instead of getting JSON properties as elements of XML, I am receiving "title":"source". The desired output should be <title>source</title>. Can you help me identify what mistake I mig ...

Updating the default value of a MUI TextField: Step-by-step guide

I am in the final stages of completing my form, but I have encountered an issue when trying to display the current user's name in the defaultValue variable of the TextField from MUI. If the value starts as ""/null, or essentially empty, the ...

What is the process for deleting an animation using JavaScript, and how can the background color be altered?

Two issues are currently troubling me. First off, I am facing a challenge with an animation feature that I need to modify within the "popup" class for a gallery section on a website. Currently, when users load the page, a square image and background start ...

Insert a scrollbar into the new popup window on Internet Explorer after the window has been opened

I am looking to enable scrolling in a pop-up window after it has been opened. While FireFox offers the 'window.scrollbars' property for this, Internet Explorer does not have a similar feature. Is there a way to add scrolling functionality in IE u ...

Is it possible to retrieve data from Local Storage using user_id and SessionId, and if so, how can it be done?

I have some data in an interactive menu created with iSpring, which includes a feature for local storage to save the last viewed page. I also have a system for logging and need to associate this local storage with user_id or sessionid. I found some informa ...

How does Ruby on Rails facilitate interactions between multiplayer users and visitors?

I am looking to create a way for visitors to interact on my website. Imagine a virtual chat space. This involves collecting and sharing data among users, which can be accomplished using ajax or similar methods. However, I'm wondering if there are ex ...

Guide to incorporating jsTree into Express applications

I just set up jsTree in my project's bower_components folder (following the steps outlined in this post). Here's how my jade template is structured: doctype html html head title= title script(src='/bower_components/jstre ...

Exploring the concept of String Enums through Reverse-Mapping

I was exploring the use of string enums in TypeScript and came across an issue with reversed mapping support. Here's what my enum looks like: enum Mode { Silent = "Silent", Normal = "Normal", Deleted = "Deleted" } I want to be able to pa ...

What is the best way to retrieve data from init.js and use it in messageView?

Can someone help me retrieve the users_url from init.js in my messageView? init.js (function(global) { "use strict;" // Class ------------------------------------------------ function Config() { var users_url = "some_url"; }; ...