我的MEAN堆栈应用程序出现问题。我是节点,角度,mongoDB,express,jade中的新手......这可能可以解释为什么我失败了......未调用Node.JS控制器
我没有找到与我的所有requiremnt匹配的单个教程,所以我从不同的教程构建此应用程序但他们从不以相同的方式访问/组织数据。有时,他们申报服务,somtimes他们使用的路由提供商...
因此,这里是我如何设置我的应用程序:
app.js
package.json
routes.js
---config
auth.js
database.js
passport.js
---node_modules
[...]
---public
+---images
+---javascripts
| | CarListController.js
| |
| \---vendor
\---stylesheets
style.css
---views
index.jade
layout.jade
login.jade
profile.jade
的问题是在我的index.jade。在这里,我想显示汽车列表(并稍后在列表中进行搜索)。
所以我创建了一个CONTROLER(CarListController.js):
function CarListController($scope, $http) {
console.log('Calling CarListController'));
$http.get('/api/cars').success(function(data) {
$scope.cars = data.cars;
});
});
在我routes.js我检索JSON汽车名单:
module.exports = function(app, passport) {
app.get('/api/cars', function(req, res) {
// load the things we need
var mongoose = require('mongoose');
// define the schema for our user model
var carSchema = mongoose.Schema({
marque : String,
modele : String,
desc : String
});
// create the model for users and expose it to our app
var Car = module.exports = mongoose.model('Car', carSchema);
// use mongoose to get all cars in the database
Car.find(function(err, car) {
console.dir(car);
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(car); // return all cars in JSON format
});
});
app.get('/', function(req, res) {
res.render('index.jade') // load the index.jade file
});
我index.jade
最后:
html(lang='fr', ng-app='LocatyvModule')
head
meta(charset='utf-8')
title My AngularJS App
link(rel='stylesheet', href='/stylesheets/style.css')
script(type='text/javascript', src='/javascripts/vendor/angular/angular.js')
script(type='text/javascript', src='/javascripts/vendor/angular-bootstrap/ui-bootstrap-tpls.js')
script(type='text/javascript', src='/javascripts/LocatyvModule.js')
script(type='text/javascript', src='/javascripts/CarListController.js')
body
div.container(ng-controller="CarListController")
div
div.row.car(ng-repeat="car in cars")
p A CAR!
当我打电话给“localhost:8080/api/cars” - >没关系我有3辆车在我的数据库中。
当我打电话给我的index.jade我没有得到3行,我没有得到控制器中的痕迹。 我做错了什么?
另外我觉得我的项目组织不是很好(如果你有索姆提示可以随意说)。
您确定结果是'$ scope.cars = data.cars;'不是'$ scope.cars = data;'。你项目布局很好。 – wayne
不工作,但问题是我没有看到我放入控制器的跟踪日志,所以甚至没有被调用。 – Tyvain
我需要在哪里声明我的控制器?有时我看到教程中有服务,是否有必要? – Tyvain