Issue with AngularJS $q factory: Unable to access property 'then' due to being undefined

I've been attempting to create a reusable auto search feature in an AngularJS factory. Below is my code snippet, but I keep encountering the error: "Cannot read property 'then' of undefined". I've searched on Google and Stack Overflow for a solution but haven't been able to resolve it.

(function() {
  'use strict'
  angular.module('App').factory('LoadFormActivity', ['$http', '$q', function($http, $q) {

    function NGAutoCompleteSearch(url, val) {

      var def = $q.defer();

      $http.get(url, {
        params: {
          searchvalue: val
        }
      }).then(function(response) {
        debugger;
        def.resolve(response);
      }, function(response) {
        debugger;
        def.reject(response);
      });

      return def.promise;
    };

    return {
      NGAutoCompleteSearch: NGAutoCompleteSearch

    };


  }]);

})();

//Controller.

(function() {

  'use strict'
  angular.module('App').controller('ServicesMasterCtrl', ['$timeout', '$scope', '$filter', '$http', 'NGAutoComplete', 'LoadFormActivity', function($timeout, $scope, $filter, $http, NGAutoComplete, LoadFormActivity) {

    $scope.FormService.AutoSearch = {};

    $scope.FormService.AutoSearch.Search = function(id) {

      LoadFormActivity.NGAutoCompleteSearch('/Assets/AssetsAPI/GetServiceRecord', id).then(
        function(data) {
          console.log(JSON.stringify(data));
        });
    };
  }]);
})();

Could someone lend a hand with this issue?

Answer №1

One way to streamline the LoadFormActivity service is by removing the unnecessary promise at the end.

function NGAutoCompleteSearch(url, val) {

    return $http.get(url, { params: {searchvalue: val} });

}

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

Is there a way to set up a local npm module directory without using symlinks

Here is a breakdown of the file structure in a simple way. /my-module ..package.json /my-app ..package.json I am trying to have my-app install my-module locally. This is what I have attempted: "dependencies": { "myModule": "../my-module" } The opti ...

Unable to execute specific php function using ajax

I have created a script that utilizes an ajax request. This script is triggered when a user clicks a button on the index.php page, like so: Risorse.php <form method='post'> Name <input type='text' name='nome&apo ...

Issues with the display property

After setting the display of a div tag as none in the style property, I am trying to show it based on an on-click function. However, the issue I am facing is that when I click on the function, the div displays for a brief moment then disappears again. Ca ...

React blogging site's administrative dashboard

https://i.sstatic.net/M6fUJ.png I am currently in the process of developing a blogging platform using MERN technology. Specifically, I am focused on creating a restful API with Node.js, Express, and MongoDB. The frontend, built with React, consists of thr ...

Step-by-step guide to swapping an element with a textarea element using javascript

My current project involves creating a user profile that includes a text box where users can describe themselves. I've already implemented a separate page for editing the profile, but now I want to add a feature where users can hover over their descri ...

Stunning Opening and Closing Animation with Ajax

Looking for help with creating an animation like the one shown here: Incorporating this into my current site at: dageniusmarketer.com/DigitalWonderland/ I want the window displaying text content to open and close as users navigate through the links, ess ...

AngularJs - receiving '404 Error: Page not found' error message after setting $locationProvider.html5Mode to true

By placing this in my .config method, the # is removed from the URL: $locationProvider.html5Mode({ enabled: true, requireBase: false }); To address the issue of manual refresh causing problems, I have come across various solutions that involve ...

Simple Way to Show API Output in Plain Text (Beginner Inquiry)

I'm a beginner when it comes to using APIs. I'm trying to display the plain text response from this URL on my webpage: However, I'm encountering issues with getting it to work. Here's my code: <script src="https://ajax.googleap ...

Displaying information from a JSON file on a Bootstrap 4 Carousel with jQuery, complete with image references

I am attempting to parse a JSON file from my local files and in console.log I can see the array, but I'm not sure how to display it in the Bootstrap carousel to showcase the data. My goal is to retrieve JSON files and only display those that start wit ...

Is it practical to extract the page type/name from the CSS of the selected tab?

My website has multiple tabs on a single page and I want to integrate Google Analytics code. However, since all the tabs are part of the same page, I need a way to track which tab the user is interacting with. Would using CSS to check for the selected tab ...

Guide to refreshing the modal component with updated properties

In my application, I have created a modal component for managing recipes. This modal allows users to save recipes to a list. let modal = <Modal saveRecipe={this.saveRecipe} closeModal={this.toggleModal}/> However, I also want to utilize the same m ...

Removing punctuation from time duration using Moment.js duration format can be achieved through a simple process

Currently, I am utilizing the moment duration format library to calculate the total duration of time. It is working as expected, but a slight issue arises when the time duration exceeds 4 digits - it automatically adds a comma in the hours section (similar ...

Customizing CSS based on URL or parent-child relationship in WordPress

I'm having trouble finding a solution for what seems like a simple issue... Currently, my header has a border-bottom and I'd like to change the color of this border based on the section the user is in. For example, consider these parent pages: ...

Transferring Specific Information to Individual Fancybox Instances

I'm currently in the process of integrating Fancybox 3 into my application. My goal is to create a custom button that performs specific functions for each instance, requiring additional data such as photoId and settingsId. To illustrate: HTML: Withi ...

How can the swipe feature be enabled for md-tabs in angular-material while using AngularJS?

I am attempting to enable the swipe feature for the md-tabs material, but have been unsuccessful. There is a parameter mentioned in the documentation called "md-swipe-content" which is a boolean - Can someone guide me on how to activate this parameter an ...

Developing a Fresh Settings Tab and Vue Module in Laravel Spark

When working with Laravel Spark, you'll find that most settings panels come with a corresponding Vue JS component that is called upon using a custom tab. <spark-create-tests :user="user" inline-template> <div> </div> </sp ...

navigating the matrix of arrays in ReactJS / JavaScript

I have a snippet of code available at this link. Currently, I am working with an array of arrays containing data. const data = [ [ { city: "Phnom Penh", country: "KH" }, { city: "T ...

Loop through the given input and output the elements if it is an array

In my controller, I need to reference a value that could be either a string or an array. If it's an array, I want to use "ng-repeat" to display each item. But if it's just a string, I want to show the string as is. I'm looking for a soluti ...

Attempting to create a functional action listener for a deck of cards game

I'm currently working on a game and want to make an image appear blank when clicked on, to simulate it disappearing. Specifically, this is for a tri peaks solitaire game. I have a function that tests the validity of playing a card, but I'm strugg ...

Angular: The module has been successfully defined, however, it has not been

I have been struggling to find a solution for a while now. I am new to Angular and attempting to convert my website into an Angular app. The app includes a controller and 2-3 directives. In the header section of the page I have: Home.cshtml: <head> ...