javascript - 无法读取未定义的属性 - 代码或服务器有问题吗?

标签 javascript json apache tomcat

我正在开发一个应用程序,该应用程序在前端使用 AngularJS,在后端使用 Java (Spring)。该应用程序发出请求 http://localhost:8080/Sprint002/restpatent/ 所以我同时运行 Apache 和 Tomcat(端口 80 上的 Apache)。

返回错误 TypeError: Cannot read property 'fetchAllUsers' of undefined。所以我能看到的是没有找到对象。 Apache和Tomcat同时运行有问题吗,如果没有,是什么原因?

app.controller('UserController', ['UserService', function($scope, UserService) {


var self = this;
self.user={id:null,username:'',address:'',email:''};
self.users=[];
self.submit = submit;
self.edit = edit;
self.remove = remove;
self.reset = reset;
console.log('hello')


fetchAllUsers();

function fetchAllUsers(){
    console.log('hello2');
    UserService.fetchAllUsers()
        .then(
        function(d) {
            self.users = d;
        },
        function(errResponse){
            console.error('Error while fetching Users');
        }
    );
}

app.factory('UserService', ['$http', '$q', function($http, $q){ 
    console.log('hello3')

var REST_SERVICE_URI = 'http://localhost:8080/Sprint002/restpatent/';

var factory = {
    fetchAllUsers: fetchAllUsers,
    createUser: createUser,
    updateUser:updateUser,
    deleteUser:deleteUser
};

return factory;

function fetchAllUsers() {
    var deferred = $q.defer();
      console.log('hello4')
    $http.get(REST_SERVICE_URI)
        .then(
        function (response) { //success
            deferred.resolve(response.data); //processes the data
        },
        function(errResponse){ //error
            console.error('Error while fetching Users');
            deferred.reject(errResponse); //rejects the data
        }
    );
    return deferred.promise;
}

[{"id":1,"applicationNumber":"N8167625","clientRef":"TER-34421","costToRenew":"534","renewalDueDate":"30/09/2017","basketStatus":"Cannot add to Basket"},{"id":2,"applicationNumber":"W71679234","clientRef":"Breitling","costToRenew":"5367","renewalDueDate":"30/11/2017","basketStatus":"Add to Basket"},{"id":3,"applicationNumber":"T7167921","clientRef":"Rolex","costToRenew":"8341","renewalDueDate":"30/05/2017","basketStatus":"In Basket"},{"id":4,"applicationNumber":"uk167444","clientRef":"Timex","costToRenew":"8341","renewalDueDate":"30/02/2017","basketStatus":"Add to Basket"}]

最佳答案

改变这个:

app.controller('UserController', ['UserService', function($scope, UserService)

为此:

app.controller('UserController', ['$scope', 'UserService', function($scope, UserService)

关于javascript - 无法读取未定义的属性 - 代码或服务器有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998471/

相关文章:

java - 如何使用 jmeter 在单个 .jmx 中运行多个测试组

javascript - 使用 JavaScript 在文本字符串中插入隐藏字符

javascript - 如何在将 ajax 发送到 php 后关闭页面重新加载以及如何修复此代码

Javascript 发出表单值和窗口

c - 如何清理由 "json_object_new_string"创建的 json 对象?

linux - 如何在 Perl 中以 root 身份运行一个句子

c++ - 为什么在我的 Apache 2.2 模块中调用 ap_hook_type_checker 函数时 request_rec::filename NULL?

javascript - 电话间隙 : manipulate files system

javascript - jQuery AJAX 调用带有 JSON 返回的 PHP 脚本

jquery - 通过ajax(jquery)显示JSON数组