2014-07-12 67 views
1

angularjs錯誤我使用的資源向服務器一個電話,當我去的對服務器的回調

/viewEvent

基本URL,它工作正常。我收到所有的數據庫條目。然而,當我去

/viewEvent/1234

,其中1234是EVENTID

我得到一個不確定是不是函數這是從內部的角度崩潰。堆棧跟蹤是

TypeError: undefined is not a function 
at copy (http://localhost:8000/js/lib/angular/angular.js:593:21) 
at http://localhost:8000/js/lib/angular/angular-resource.js:410:19 
at wrappedCallback (http://localhost:8000/js/lib/angular/angular.js:6846:59) 
at http://localhost:8000/js/lib/angular/angular.js:6883:26 
at Object.Scope.$eval (http://localhost:8000/js/lib/angular/angular.js:8057:28) 
at Object.Scope.$digest (http://localhost:8000/js/lib/angular/angular.js:7922:25) 
at Object.Scope.$apply (http://localhost:8000/js/lib/angular/angular.js:8143:24) 
at done (http://localhost:8000/js/lib/angular/angular.js:9170:20) 
at completeRequest (http://localhost:8000/js/lib/angular/angular.js:9333:7) 
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8000/js/lib/angular/angular.js:9303:11) angular.js:575 

當我檢查服務器時,請求是正確的。我可以看到它得到了1234,並從mongo數據庫中提取了正確的條目。

這是控制器邏輯

.controller("viewEventsController", ["$scope", 'EventService', '$location', function($scope, EventService, $location){ 

    var path = $location.path().split('/'); 
    var pathSize = path.length; 
    $scope.events = []; 

    if(pathSize === 2){ 
     console.log("No event ID"); 

     $scope.events = EventService.query(); 

    } 
    else{ 
     console.log("Event ID specified"); 
     EventService.get({"eventID": path[pathSize - 1]}, function(data){ 
      //$scope.events.push(data); 
      console.log(data); 
     }, function(error){ 
      console.log(error); 
     }); 
    } 
}]); 

和業務邏輯

service.factory('EventService', function($resource){ 
    return $resource('api/viewEvent/:eventID'); 
}); 

它永遠不會讓回給控制器,所以我「有信心」這並不是說。 (看它是)

+0

看看這個答案正確使用傳遞參數到$資源(eventID)。 http://stackoverflow.com/questions/19365714/how-to-pass-in-parameters-when-use-resource-service –

回答

0

不知道的最好的方式,但我得到了它做

在服務工作:

service.factory('EventService', function($resource){ 
    return $resource('api/viewEvent/:eventID', 
     {eventID:"@eventID"}, 
     { 
      'getSingleEvent': { 
      url: "api/viewEvent/:eventID", 
      method: "GET", 
      isArray: true 
      } 
     } 
    ); 

控制器

var path = $location.path().split('/'); 
var pathSize = path.length; 

EventService.getSingleEvent({"eventID":path[pathSize - 1]}, function(result){ 
     $scope.updateEvent(); 
    }); 

服務器

routes = require('./routes') 
var router = express.Router(); 
router.get('/api/viewEvent/:eventID', routes.viewEvent); 

並在潰敗中es目錄我有一個js文件與

var mongoose = require('mongoose'); 
var db = mongoose.createConnection('localhost', 'eventApp'); 
var eventSchema = require('../models/createEvent.js').eventSchema; 
var event = db.model('events', eventSchema); 

exports.viewEvent = function(req, res){ 

console.log(req.params.eventID); 

if(req.params.eventID) { 
    event.find({"_id": req.params.eventID}, function (error, events) { 
     console.log(events); 
     res.send(events); 
    }); 
} 
else{ 
    event.find({}, function (error, events) { 
     console.log(events); 
     res.send(events); 
    }) 
} 
};