2016-04-19 9 views
1

getRooms関数に関しては、私のmongooseモデルで概説されているように、room.Name、moderator、およびdescriptionを含むオブジェクトの配列の部分ページ読み込み(/ rooms) (Room)とdbのデータを使って、この情報をページにレンダリングすることができました。代わりに私はクライアントサイドでのレスポンスとしてindex.htmlコードが表示されているようにコンソールにログオンしています。サーバーには決して到達しません。私のPOSTリクエストとPUTリクエストは機能していますが、これは基本的なことですが、このGETリクエストを正しく作成する方法を理解できていないようです。どのようにこれが正しく行われたかについて誰かが私に知らせることができれば、私はそれを感謝するでしょう。mongooseとangularを使用した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

あなたの*ルートは、あなたの/ roomルートの前にサーバー上で起こりますか? – ceckenrode

+0

うん、それだった –

関連する問題