0
我正試圖在MongoDB中顯示一些數據到前端。我正在使用MEAN堆棧,這是我相對較新的。顯示來自MongoDB的數據(MEAN堆棧)
我已成功獲取數據以本地主機URL(本地主機:3030 /事件)以JSON格式顯示,但無法獲得乾淨的數據顯示在前端(位於localhost:9000)。
這是我的代碼看起來像迄今在服務器端(router.js):
"use strict";
var config = require('../config/database-config');
var Router = function (options) {
var self = this;
self.environment = options.environment;
self.route = function(app) {
var IncidentController = require('./controllers/incident-controller.js');
var IncidentModel = require('./models/incident-model.js');
var incidentModel = new IncidentModel(config[`${self.environment}`]);
console.log("Incident Model", incidentModel);
var incidentController = new IncidentController({model: incidentModel});
app.get('/api/incident/:id', incidentController.findIncidentById);
app.get('/incidents', incidentController.getAllIncidents);
};
return self;
};
module.exports = Router;
客戶端:
services.js:
'use strict';
angular.module('victimList.services', []).factory('Victim', function($resource) {
return $resource('http://localhost:3030/api/incident/:id');
});
data.js:個
'use strict';
angular.module('victimList', [])
.service('victimList')
.controller('DataCtrl', ['victimService', function ($scope, victimService) {
victimService.getVictims(function(victims) {
$scope.victims = victims;
});
}])
.factory('victimSerivce', function($http) {
var getVictims = function(callback) {
$http.get('http://localhost:3030/incidents').success(function(data){
callback(data);
});
};
return {
getVictims: getVictims
};
});
app.js:
'use strict';
angular
.module('frontendApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'victimList',
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.when('/victims', {
templateUrl: 'views/victims.html',
controller: 'DataCtrl',
controllerAs: 'victims'
})
.otherwise({
redirectTo: '/'
});
});
victims.html:
<div ng-app="victimList">
<h1>Testing</h1>
<div ng-controller="DataCtrl">
<ul ng-repeat="victim in victims">
<li>1</li>
<li>{{ victim.firstname }} {{ victim.lastname }}</li>
</ul>
</div>
當我瀏覽到本地主機:9000 /受害者頁面,我得到的頭標記,但沒有數據。控制檯顯示此錯誤:
Error: [$injector:unpr] Unknown provider: victimServiceProvider <- victimService <- DataCtrl
我可能會丟失什麼?在此先感謝您的幫助!
你缺少的函數調用之前你DataCtrl範圍增加。 –
你有一個錯字。您將您的服務命名爲victimSerivce,並在您的控制器中引用'victimService'。 :) – eminlala
@MatthewGreen,我將如何添加範圍? 謝謝,@ ex0dm3nt !!我修正了錯字,但現在顯示錯誤:'TypeError:無法讀取未定義的屬性'getVictims' –