Using ui-router to pass promise results to nested resolves

I am currently using ui-router with nested resolve configurations like this :

    $stateProvider
         .state('route', {
             url: '/route',
             templateUrl: 'mypage.aspx',
             resolve: {
                 getjson: function (readJson,$http) {
                     var readjsonget = $http.get('files.json');
                     readjsonget.then(function (result) {
                         return result.data;                             
                     }, function (ex) {
                         console.log('GET error', ex);
                     });                         
                 },
                 load: function ($q, filesInject, getjson) {
                     files = getjson;
                     $log.info(2);
                     return $q.all(files.mape(filesInject.inject));
                 }
             }
         })

I have been attempting to pass the content of a JSON file to the second resolve function.

Despite trying multiple approaches, I consistently encounter the issue where the promise success is only triggered after the second function execution.

Any suggestions on how I can address this dilemma?

Answer №1

Make sure to include a return statement in the promise readjsonget.

$stateProvider
     .state('route', {
         url: '/route',
         templateUrl: 'mypage.aspx',
         resolve: {
             getjson: function (readJson, $http) {
                 var readjsonget = $http.get('files.json');
                 return readjsonget.then(function (result) {
                     return result.data;                             
                 }, function (ex) {
                     console.log('GET error', ex);
                 });                         
             },
             load: function ($q, filesInject, getjson) {
                 files = getjson;
                 $log.info(2);
                 return $q.all(files.map(filesInject.inject));
             }
         }
     })

Answer №2

How about trying a code snippet like the following:


    $stateProvider
      .state('page', {
          url: '/page',
          templateUrl: 'mywebsite.php',
          resolve: {
              fetchData: function (fetchData, $http) {
                  var deferred = $q.defer();
                  var requestData = $http.get('data.json');
                  requestData.then(function (response) {
                      deferred.resolve(response);          
                  }, function (error) {
                      deferred.reject(error);
                  });          
                  return deferred.promise;               
              },
              process: function ($q, dataHandler, fetchData) {
                  var deferred = $q.defer();
                  content = fetchData;
                  $log.log(3);
                  content.then(function(result){
                      deferred.resolve(result.map(transformData));
                  }, function(err){
                      deferred.reject(err);
                  });
                  return deferred.promise;
              }
          }
      })

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

Angular: Displaying the unique identifiers of a single MongoDB record

I am facing an issue with the code snippet in my HTML file: <li ng-repeat="friend in friends"> <span ng-repeat="(key, value) in friend"> <input type="text" ng-model="friend[key]"> </span> ...

Freshening up the data source in a Bootstrap Typeahead using JavaScript

I'm trying to create a dropdown menu that displays options from an array stored in a file called companyinfo.js, which I retrieve using ajax. The dropDownList() function is called when the page loads. function dropDownList (evt) { console.log("dr ...

The route you are trying to access is currently unavailable

I've successfully developed a web app, and now I need to replicate the same HTML page with a slightly different controller. Despite my attempts to utilize ngRoute, it's not functioning as expected, and I'm uncertain if my route setup will yi ...

Comparing $(document).width() to document.body.clientWidth

When it comes to fixing CSS issues on the mobile version of a responsive site, I'm torn between two declarations. Some tutorials recommend using $(document).width(), while others suggest using document.body.clientWidth. The former belongs to jquery, w ...

Angular service that iterates through each $q.promise

Here's the function I am working with: this.getBenchmarkData = function () { var benchmarkData = []; var d = $q.defer(); users.forEach(function(user){ var dataArray = []; modules.forEach(function (module) { ...

How can I prevent the same function from being triggered in an AJAX call and a .scroll event when I click on another div with an onclick attribute

I have encountered an issue with my code where two div tags each contain a different onclick function call. The problem arises when I click on one of them and then proceed to click on the other - the .scroll(function() seems to be triggered by both functio ...

Ways to modify babel output file extensions

I'm facing an issue where babel --out-file-extension is not working as expected. Below is my package.json : { "name": "Assets", "version": "1.0.0", "description": "", "main" ...

Error: Attempting to access a property of an undefined object using method chaining

I encountered an error of property undefined with the code below. I'm not sure what's causing it. I checked by doing a console.log(navList) in the render and it does have a value. Even after adding if(!navList) return null, I still get the same e ...

Set up the FrontEnd and BackEnd on distinct services within the Google Cloud Platform for deployment

I'm facing a challenge with deploying two applications on GCP. The first application is the backend API running on port 8080, and the second is the front end running on port 4200. After dockerizing both applications and uploading them to GCP, I crea ...

Guide on displaying data from a list in aspx with javascript/Jquery from aspx.cs

Code in aspx.cs file protected void Page_Load(object sender, EventArgs e) { foreach (UserDetail l in liststUser) { UserName = l.Name; ...

Issue with running bowerInstall in Grunt

After starting a new AngularJS project for the first time, I've encountered some challenges... My latest issue revolves around configuring bower properly in my index.html file. When I manually hardcode things to the googleapis, everything works fine ...

Issue with Component: Data is not being injected into controller from ui-router resolve, resulting in undefined data

Encountering an issue with resolve when using a component and ui-router: the data returned after resolving the promise is displaying as "undefined" in the controller. Service: class userService { constructor ($http, ConfigService, authService) { th ...

Error: Firebase is throwing an error stating that it cannot access the length property of an undefined property

I need help establishing a connection with Firebase. Here is the code snippet from firebase.js: import * as firebase from "firebase/compat/app"; const firebaseConfig = { apiKey: "***", authDomain: "***", projectId: &qu ...

For each error that occurs when there is only one item in the array during a post request

My nodejs server has a JSON schema like this: var UserSchema = new Schema({ nick: String, deviceId: String, visivel: Boolean, checks: [{date: {type:String},log: {type:Number},lng: {type:Number}}] }); In the Post code ...

matching equivalent URLs to identical states using an optional parameter in angular-ui-router

I am looking to map the following states to the same routes with a parameter: /en/live/ /en/ /en/live/football /en/football The goal is to store the 'live' part in a variable. I have attempted the following approach: $stateProvider.state(&ap ...

Find the variance between the stored date in JSON and the current date using AngularJS

I'm facing a challenge with saving dates (last checkup) in a json file and calculating the time between now, so I can inform users when they need a new checkup (yearly). I attempted a simple solution, but it doesn't seem to be functioning as exp ...

Utilizing jQuery for easy drag-and-drop functionality in web development

I have two lists which are currently using jQuery for functionality. An example can be found here. I need the same basic functionality but with some modifications: Instead of just getting all sortable items by clicking "Get items," I want to be able to dr ...

sticky navigation causing issues with tab functionality

Currently in the process of developing a website with a rather intricate sticky navigation bar. The JavaScript code being utilized to achieve this functionality is as follows: // Code for creating a sticky navigation bar document.addEventListener("s ...

Alter the input background color steadily through javascript transformation

Is there a way to gradually fade in a new background color using JavaScript only? I am currently changing the background color with the following code: // html <input onfocus="onFocus(this)" onblur="onFocus(this)" type="text"> // JS function onFoc ...

Regular expressions for identifying operands and operators in a calculator application implemented with JavaScript

I am currently working on a JavaScript project to create a basic calculator. I am in need of a way to validate each item in an array of operands (sequences of digits) and operators (+, -, *, /, ** for power). I have managed to create regex patterns for di ...