Error: Unable to split function. Attempting to retrieve API response via GET request using ngResource

I am trying to retrieve some data from an API using ngResource by utilizing the get method.

Even though I have set up a factory for my resource, when implementing it in my controller, I encounter an error stating URL.split is not a function. I'm struggling to identify the issue within my code.

var Myapp = angular.module('starter.controllers', ['ngResource'])
    .config(['$resourceProvider', function ($resourceProvider) {
            $resourceProvider.defaults.stripTrailingSlashes = false;
        }]);
Myapp.factory('Users', function ($resource) {
    return $resource('some URL', {}, {
       query: {
          method: 'GET'
       }
    });
});
Myapp.controller('DashCtrl', ['$scope', '$state', 'Users', function ($scope, $state, Users) {
    Users.query().$promise.then(function (data) {
        alert(JSON.stringify(data, null, 4));
    }, function (error) {
        console.log('Error is: ' + JSON.stringify(error, null, 4));
    });

}])

Answer №1

you provided 3 items('$scope', '$state', 'Users') as dependencies but are passing 4 items to the function ($scope, $state, $http, Users). Consider removing $http

Myapp.controller('DashCtrl', ['$scope', '$state', 'Users', function ($scope, $state, Users) {
    Users.query().$promise.then(function (data) {
        alert(JSON.stringify(data, null, 4));
    }, function (error) {
        console.log('Error is: ' + JSON.stringify(error, null, 4));
    });

}])

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

Display a sublist when a list item is clicked

I am receiving a JSON object in my view that looks like this: $scope.mockData = [ { "folder": "folder1", "reports": [{ "name": "report1" }, { "name": "report2" }, { "name": "report3" }] }, { "folder": "folder2", "reports": [{ "name": ...

Comparison of element state prior to and post editing (with contentEditable)

Exploring how elements within a div can be monitored for changes made by the user (thanks to contentEditable), I created a sample page with the following setup: before_html = $("#example_div").children(); $("#differences_button").on("click", ...

Issue with NPM identifying my module (demanding unfamiliar module)

Currently, I am developing a React-Native application and organizing my components into separate files. Most of the time, this method works perfectly fine except for one specific instance where I keep encountering a 'Requiring unknown module' err ...

Is there a way to ensure that my AngularJS factory fetches HTTP data only once?

I have implemented a factory in my project to share data among multiple controllers. Here is the code for my factory: var szGetData = "some url that works"; myApp.factory('Data', function ($http) { var eventData = {}; eve ...

Enhance your HTML rendering with Vue.js directives

Check out this cool code example I created. It's a simple tabs system built using Vue.js. Every tab pulls its content from an array like this: var tabs = [ { title: "Pictures", content: "Pictures content" }, { title: "Music", c ...

What is the best way to sort through this complex array of nested objects in Typescript/Angular?

tableData consists of an array containing PDO objects. Each PDO object may have zero or more advocacy (pdo_advocacies), and each advocacy can contain zero or more programs (pdo_programs). For example: // Array of PDO object [ { id: 1, ...

What is preventing me from injecting a directive into my test cases?

While I have been successful in injecting various things in the beforeEach(inject(beforeEach(inject(function(_$controller_,_mycustomService_,_$log_) in Jasmine, there is one challenge I'm facing when trying to inject a directive. Despite being able ...

AJAX requires manual updating by clicking on a button

I've created a chat system where two individuals can communicate with each other. An AJAX function has been implemented to update the message container every 2 seconds. However, I have noticed that the AJAX call doesn't run immediately after a u ...

Utilizing AngularJS: Binding stateParams value to custom data within state objects

Following the guidelines here, I am setting a page title in my state object. $stateProvider .state('project', { url: '/projects/:origin/:owner/:name', template: '<project></project>', data : { pageTi ...

I possess 9 captivating visuals that gracefully unveil their larger counterparts upon being clicked. Curiously, this enchanting feature seems to encounter a perplexing issue within the realm of web browsing

<style type="text/javascript" src="jquery-1.11.0.min.js"></style> <style type="text/javascript" src="myCode.js"></style> </body> //jquery is within my site directory on my desktop $(document).ready(function(){ //note: $("#ar ...

A guide on incorporating the close button into the title bar of a jQuery pop-up window

Check out this fiddle: https://jsfiddle.net/evbvrkan/ This project is quite comprehensive, so making major changes isn't possible. However, the requirement now is to find a way to place the close button for the second pop-up (which appears when you c ...

Tips for revealing a hidden div by clicking on another div?

I have a Google visualization chart inside a div tag. I would like to display this chart in a pop-up box when clicking on the chart's div. I am utilizing jQuery for this feature. ...

Angular Directive: Encountering issues with binding to model properties

I am currently working with Angular to develop a straightforward directive. My goal is to showcase the model properties x and y as attributes within the directive. However, instead of retrieving the values for x and y from scope.textItems, I am only seeing ...

Is it possible to integrate Vue.js within a web component?

Is it possible to utilize VueJS to control behavior within a web component? In other words, if the VueJS library is included as a script reference, can it be integrated in the same way as on a standard HTML page, but within the confines of a web componen ...

Testing the functionality of an Express.js application through unit testing

I'm currently working on adding unit tests for a module where I need to ensure that app.use is called with / and the appropriate handler this.express.static('www/html'), as well as verifying that app.listen is being called with the correct p ...

AngularJS: Enhancing Date Display and Organization

Looking to change the date format from "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)" to "YYYY/MM/DD" within my controller. ...

Discover updates within a JQuery Ajax call

I am sorry if this question sounds simple, but I would like to know how to set up a function that triggers when the Ajax data changes from the previous request. window.setInterval(function(){ $.get("feed", function(data){ if (data.changed ...

Enhancing Symfony's performance through optimized Ajax response time

When using Symfony2, I am experiencing differences in loading times for AJAX requests between development and production environments. In development, it takes 1 second to load, while in production it only takes 500 milliseconds for a simple call: Here is ...

"Encountering timeout issues with Angular's resource module

I've encountered an issue where a database query called from the backend through the page controller seems to cause an error if it doesn't return immediately. I'm not sure whether the problem lies with Angular or Node.js. Just so you know, ...

Tips for closing two nested Material-UI popovers when a button is clicked or when clicked elsewhere

Trying to create a menu with nested popovers from Material-ui has presented a challenge. I want all the popovers to close when I click on a menu option, rather than having to close them individually. Additionally, it would be more user-friendly if the popo ...