使用mongoose和angular获取请求格式

时间:2016-04-19 19:15:00

标签: angularjs mongodb express http-get

关于getRooms函数,我期望在部分页面加载(/ rooms)上的console.log包含我的mongoose模型(Room)概述的包含roomName,版主和描述的对象数组,以及db,以便我可以将一些信息呈现给页面。相反,我是控制台记录似乎是我的index.html代码作为客户端响应,从未达到服务器。我的POST和PUT请求正在运行,虽然这是初步的,但似乎我不理解如何正确地进行此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 个答案:

答案 0 :(得分:1)

检查您的服务器端路由。您正在记录index.html页面,因为您的请求没有达到任何快速路由。所以它会点击app.get(*)路由并返回index.html页面的html。确保一切拼写正确,除非你的意思是

,否则你在另一端使用get而不是帖子