The AngularJS factory's HTTP GET method returns the error message "undefined is not a function."

I've encountered an error in my code that reads:

TypeError: undefined is not a function
. It seems to be related to my understanding of promises. I would really appreciate any insights or explanations on this issue.

getProgramDetails:  function (program) {
    var _this = this;
    this.getDetails(program).then(function() {
        ....
    });
},
getDetails: function (program) {
    var _this = this;
    var deferred = $q.defer();

    // The error is triggered at this line
    this.http.get({id: program.programID}).then(function(results) {
        if (results && results.programID) {
            _this.isNewProgram = false;
            _this.selectedProgram = {
                ...
            };
        } else {
            _this.isNewProgram = true;
            _this.selectedProgram = {
                ...
            };
        }
        deferred.resolve();
    });
    return deferred.promise;
},

http: $resource(
$window.detailsEndpoint',
{ id: '@id' },
{   //parameters default
    update: {
        method: 'PUT',
        params: {}
    },
    get: {
        method: 'GET',
        params: {
            id: '@id'
        }
    },
    post: {
        method: 'POST'
    }
})

Answer №1

It seems that $resource does not support a .then() method like $http. Instead, consider passing the callback as the second argument.

this.http.get({id: program.programID}, function(response){});

Alternatively, you can use its $promise.

this.http.get({id: program.programID}).$promise.then(function(response){});

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

React Components Changing Route Leads to Empty Display

My current challenge involves setting up dynamic routing in my application to enable the viewing of products from different stores. Essentially, I am developing an app where users can add stores and browse through their items. I have implemented a dynamic ...

Modifying webpage design using JavaScript for styling

Is there a way to change the css style(defined in the page source) dynamically with Java? I know it is possible to do it with JavaScript. If there isn't, are there other situations where JavaScript is the only choice developing a web app? ...

Leveraging external references within Symfony2/Twig using JavaScript

After reviewing the following post: Using javascript in Symfony2/Twig I am curious, is it feasible to incorporate scripts hosted on external servers, such as google-api? ...

Seeking a solution for resolving the problem with HTML1402 when using jquery URL in Internet Explorer?

Why is it that only IE (and not any other browser) gives me the error HTML1402: Character reference is missing an ending semi-colon “;” with this code: <!DOCTYPE HTML> <html> <head> <script src="http://code ...

How to stop the mobile keyboard from hiding in JavaScript

On a webpage, I have an HTML setup with an editor and buttons positioned above the keyboard: ------------ |1| Hello | |2| World | | | | | | | ------------ |Tab|( ) [ | ------------ |qwertyuiop| | asdfghkl | | zxcvbnm | | [ ] | ------- ...

How to access a variable in an Angular Factory's callback function from outside the factory

Here's a look at the structure of My Factory: .factory('MyFactory', function(){ return: { someFunction: functon(firstParam, secondParam, resultObject) { $http.get(url).success(resultObject); } ...

Optimal approach: Utilizing JSON, JQuery, and Codeigniter to organize content within multiple tabs

In the process of developing a calendar list system, I have implemented tabbed dates at the top, with corresponding data listings underneath. While I am comfortable using JSON and JQUERY to load the data into a div, I am uncertain about how to dynamically ...

steps for eliminating specific words from an input field

In my form, I have 13 input boxes where users can only type text and numbers. I am using the following script to enforce this rule: RESTRICT INPUT TEXT $(function() {//<-- wrapped here $('.restrict').on('input', fun ...

convert json formatting to a string

Can anyone assist me in resolving this issue? I am trying to achieve the following format: true: 2,3,4 false: 5 I am using sequelize, I am struggling with outputting JSON correctly Please see my code snippet below. However, it is not functioning as ex ...

Showing JavaScript code as a string within a PHP file

I need help with echoing JavaScript code in PHP without it being executed. In my temp.php file, I have the following code: <?php if (count($_POST) > 0) { $text = $_POST['text']; echo $text; }?> <html> <head> </he ...

"Troubleshooting a minor jQuery problem - update on current URL and refresh the page

I have developed a jQuery script that is designed to search through a webpage for specific text and then perform an action based on the findings. My query is straightforward. If I want to apply this script to a current website, such as www.google.com, how ...

Sending information to a component through navigationPassing data to a component

When attempting to move from one component to another page using routes and needing to send parameters along with it, I have experimented with passing them as state through history.push(..) like the following code. However, it does not seem to be working a ...

React - triggering a router link action before the link is assigned a value

In this scenario, the issue arises because the URL changes before the link receives a value. The state is being received directly through the component but by the time it happens, the component has already been clicked. This results in the value being as ...

Steps for accessing the files uploaded in a React application

Looking to implement an upload button using material UI that allows users to upload multiple files, with the goal of saving their paths into an array for future use. However, I'm unsure about where these uploaded files are stored. The code snippet be ...

Why is the raisePropertyChanged Event important in programming?

I was confused about the purpose of this particular event based on the official documentation. This event is commonly utilized in developing controls with client-side support (IScriptControl). get_highlightCssClass: function() { return this._hig ...

What is the best way to combine arrays fetched from two different Axios API Calls?

Currently, I am faced with a challenge while writing an Express API call. The situation involves making multiple Axios API calls within the same call, and I seem to hit a roadblock. The concept behind the Express API call is that it receives a query param ...

Changing the Angular form based on the value selected from the drop-down menu

I am currently working on a Reactive form in Angular 7 that includes 2 dropdowns (select) which are supposed to function as cascading dropdowns. However, I am facing an issue where the cascading dropdowns are not working as intended. Whenever I select an o ...

Ways to prevent scrolling or switching to the next tab when my javascript function yields a false result

//HTML Code <div id="navigation1"> <ul> <li><a href="#"><input type="submit" value="Next" onClick="return checkfhname()" /></a></li> </ul> </div> Whenever my Javascript function checkfhname() r ...

Refresh Google Maps without showing gray areas on the screen

Currently, I am utilizing the Google Maps API v3 in javascript and frequently reloading the map using an app.get while also incorporating layers and bookmarks via mongodb. Whenever I reload the map to erase everything, a gray background appears in the div ...

issue with deploying a software package and getting it installed

I developed a basic package containing only a simple <div> x </div> and published it using npm publish. When I attempted to install it in a project using npm i, I encountered the following error message: Support for the experimental syntax &apo ...