Expect a promise to be resolved in the RootCtrl of Angular using $http

One of the functions in my RootCtrl is responsible for calling an http api and returning the result.

$scope.checkAccess = function(){
    var result = MyService.me();
    result.then(function(response){
        console.log(response);
        if (response.data != 'false'){
            return true;
        }
        else{
            return false;
        }
    });
}

However, when I attempt to execute this method in a child controller using...

var access = $scope.checkAccess();

it states that access is undefined.

I'm puzzled as to what mistake I've made here?

This is how the Service call is structured:

me: function() {
    return $http({
        url: 'http://localhost:5000/api/me',
        method: 'GET'
      });
}

Answer №1

Don't forget to always remember to fulfill the promise object.

Take a look:

$scope.validateUserAccess = function(){
    var output = MyService.verifyUserInfo();
    return output.then(function(response){
        console.log(response);
        if (response.data != 'incorrect'){
            return true;
        }
        else{
            return false;
        }
    });
}

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

Cypress: Could not locate the element within the cypress environment

The content within the iframe is as follows: it.only('should verify xyz tooltips',()=>{ cy.visit('/'); cy.get('a[ng-reflect-router-link="abc"]').click(); cy.wait(9000) cy.get('iframe&apos ...

What does the $value property in angularfire refer to?

Having some trouble retrieving a value from my firebase using angularjs and angularfire. Here is the code snippet from my controller: $scope.componentStatus = syncData("components/-JI_JgHxm0TEUEVjADJn/status"); console.log($scope.componentStatus); After ...

Learn how to display a web page in a tab view similar to the toggle device toolbar option

Is it possible to simulate the toggle device toolbar of a web page using JavaScript? https://i.stack.imgur.com/M9oB0.png For example, can we set up a webpage to always display in tab or mobile view like on YouTube? ...

Having trouble with my Bootstrap 4 navbar button - what am I doing wrong?

After conducting research online, I found myself unable to resolve my issue due to my lack of proficiency in English. I apologize for any confusion caused and would like to revisit the topic. The problem lies with my navbar toggle that is not functioning ...

An illustration of webpack 4 and Vue.js compatibility tailored specifically for Internet Explorer 11, featuring multiple entry points

This feels like déjà vu - yet another issue with Internet Explorer and webpack. I'm on the brink of deploying my project when IE 11 decides to mess everything up. I thought I had covered all bases with babel-polyfill and the latest versions, but of ...

VueJS 3 - Issue with applying the <router-link-active> class to routes that share the same path starting name

In my navigation bar, I have created 3 links which are all declared at the root level of the router object. I've applied some simple styling to highlight the active link on the navbar using the <router-link-active> class. Everything works as exp ...

Array in Javascript reset to null after its second iteration

''' const users = [] const addUser = ({ id, username, room }) => { // Clean the data username = username.trim().toLowerCase() room = room.trim().toLowerCase() // Validate the data if (!username || !room) { r ...

What could be the reason for the meta viewport not functioning properly on Android devices with Cordova 5.1.1?

Ever since upgrading my app to cordova 5.1.1, I've noticed that the meta viewport no longer functions properly on Android devices. The app is not displaying correctly as a result. Despite trying various solutions, I am still facing this issue. Is ther ...

How can I configure my React Project to direct users to example.com/login.html when they land on the root URL?

My goal is to verify a user's identity through a third-party authentication server. The redirect_uri indicates that after the user logs in, they will be redirected to example.com/login.html. Inside the login.html file, there will be specific html/scr ...

Issue encountered when trying to insert an array in JavaScript into MongoDB using Mongoose

I am currently learning about node.js and mongodb. I attempted to insert a JavaScript array variable into mongodb using mongoose, but encountered an error. Upon running this code, I received the following error message: ValidationError: CastError: Cast t ...

What method is the most effective for preloading images and stylesheets?

One of the main concerns I have is optimizing the load time of my website. I would like to preload images, style sheets, and scripts to ensure a faster loading speed and to prevent users from seeing images loading right before them. Can someone suggest the ...

Display the retrieved information from MongoDB onto a jade template

Upon checking the console, I see output similar to this: [ { __v: 0, city: 'on1', address: '111', first: 'user', last: 'one', chart: 'char1', doctor: 'doc1', _id: 5698a803d98f05482ba48a4b }, { __ ...

ReactJS - What is the best way to output a string from a functional component?

Utilizing @apollo/client in my React project for handling backend APIs. Within the file appollo.js, I am attempting to make a call to the backend API link based on certain conditions. Currently, appollo.js consists solely of functions and is not considere ...

Implementing ng-submit in AngularJS to display results in a formatted table

The goal is to input some data that will update in {{urname}} and {{urcm}} within the table. However, the expression is not functioning as expected. Additionally, I am unable to open a new row in the table when submitting new comments. All the data appear ...

Please provide your credentials for the Angular JS $http GET request

I am facing a 401 Unauthorized error when trying to access an API from the Angular JS front end. I have attempted some options in order to resolve this issue. Option 1: $http({ method: "GET", xhrFields: { withCredentials: true }, ...

Determine the current directory being called in Grunt, without confusing it with the directory of the gruntfile

If Grunt is installed in a folder called /foo, but my current directory is /foo/bar/baz, and I execute "grunt sometask" from within my current directory, how can I programmatically determine the path of the current directory where Grunt was called? In othe ...

Is there a way to optimize the re-rendering and redownloading of images files in map() when the useState changes? Perhaps we can consider using useMemo

This Chat application is designed with channels similar to the Slack App. Currently, I am utilizing a map() function for filtering within an array containing all channel data. The issue arises when switching between channels, resulting in re-rendering and ...

Tips for creating multiple full-screen overlays in HTML

I am new to the world of web development and I am currently working on implementing a set of buttons that will trigger specific overlays when clicked. I found the following code snippet on W3schools which creates a button along with an overlay effect. < ...

Tips for transforming a hover menu into a clickable menu

The scenario above demonstrates that when hovering over the dropdown menu, it displays a submenu. However, I want the submenu to be displayed after clicking on the dropdown text. Could anyone provide assistance on how to change the hovermenu to a clickabl ...

Capture a photo within your app using the Cordova Camera plugin and seamlessly upload it to Parse.com platform

I am currently in the process of developing an app using Ionic/Cordova that utilizes Parse.com as a Backend as a Service. The app integrates the ngCordova Camera plugin for managing the device camera functionality. The main objective is to enable users to ...