【问题标题】:angularjs error on server callback服务器回调上的angularjs错误
【发布时间】:2014-07-16 23:49:47
【问题描述】:

我正在使用资源调用服务器,当我转到的基本 URL 时

/viewEvent

它工作正常。我收到所有数据库条目。但是,当我去

/viewEvent/1234

其中 1234 是 eventID

我得到一个 undefined is not a function,这是 Angular 内部的崩溃。堆栈跟踪是

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');
});

它永远不会回到控制器,所以我“有信心”不是那样的。 (看着它)

【问题讨论】:

标签: node.js angularjs


【解决方案1】:

不确定是否是最好的方法,但我通过这样做得到了它

在役:

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);

在路由目录中我有一个 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);
    })
}
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多