2016-04-19 33 views
1

關於getRooms函數,我期望console.log,部分頁面加載(/ rooms),包含roomName,主持人和描述的對象數組,如我的mongoose模型(房間)和數據庫中的數據,以便我可以將部分信息呈現給頁面。相反,我是控制檯日誌記錄什麼似乎是我的index.html代碼作爲響應在客戶端和服務器永遠不會到達。我的POST和PUT請求正在工作,雖然這是基本的,但似乎我並不瞭解如何正確執行此GET請求。如果有人可以告訴我這是如何正確完成的,我將不勝感激。使用貓鼬和角度的GET請求格式

//roomController.js 
    angular.module('chatApp').controller('roomController', ['$scope','$http','$location', '$cookies', function($scope, $http, $location, $cookies){ 

    // $scope.rooms = [ 
    // {'name': 'Biology', 'description': 'Discuss the wonders of Bio'}, 
    // {'name': 'Literature', 'description': 'From Steinbeck to Shakespeare'}, 
    // {'name': 'Dark Souls 3', 'description': 'Discuss gameplay from DS3'}, 
    // {'name': 'The Life of Pablo', 'description': "Discuss Kanye West\'s the Life of Pablo"}, 
    // {'name': 'Daredevil', 'description': 'Discuss the Netflix original Daredevil'}, 
    // {'name': 'React JS', 'description': 'Discuss ReactJS projects'} 
    // ]; 


    $scope.getRooms = function(){ 
     $http.get('/rooms').then(function(response){ 
     $scope.roomCount = response.data.length; 
     console.log(response.data.length); 
     console.log(response); 
     }); 

    }; 

    $scope.createRoom = function(){ 
    var newRoom = { 
     roomName: $scope.roomName, 
     moderator: $cookies.get('currentUser'), 
     description: $scope.roomDescription 
    }; 
    $http.post('/createRoom', newRoom).then(function(){ 
     $scope.roomName = ''; 
     $scope.moderator = ''; 
     $scope.description = ''; 

     $location.path('/createRoom'); 

     bootbox.alert('Sucessfully created Room.'); 
    }); 
    }; 


}]); 


//server side route 
//get rooms 

app.get('/rooms', function(req,res){ 
    Room.find({}, function (err, rooms) { 
     res.send(rooms); 
     console.log(rooms); 
     }); 
    }); 


//relevant part of partial page 
    <div class="container-fluid" id="roomsPage" data-ng-init="getRooms()"> 

回答

1

檢查您的服務器端路由。你是在記錄index.html頁面,因爲你的請求沒有擊中任何明確的路線。所以它會碰到app.get(*)路由並返回index.html頁面的html。確保一切拼寫正確,你使用得到另一端,而不是一個職位,除非你的意思是

+0

是的,看起來像兩個都是GET,一切似乎拼寫正確,其他看起來不對? –

+0

您的*路線是否在您的服務器上的/房間路線之前發生? – ceckenrode

+0

是的,就是這樣 –