我正在使用资源调用服务器,当我转到基本 URL 时
/viewEvent
效果很好。我收到所有数据库条目。然而,当我去
/viewEvent/1234
其中 1234 是事件 ID
我得到一个未定义不是一个函数,这是来自 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 逻辑
.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');
});
它永远不会返回到 Controller ,所以我“确信”事实并非如此。 (注意是这样)
最佳答案
不确定这是否是最好的方法,但我通过这样做让它发挥作用
服务中:
service.factory('EventService', function($resource){
return $resource('api/viewEvent/:eventID',
{eventID:"@eventID"},
{
'getSingleEvent': {
url: "api/viewEvent/:eventID",
method: "GET",
isArray: true
}
}
);
Controller
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);
})
}
};
关于node.js - 服务器回调上的 angularjs 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709469/