【发布时间】:2014-06-05 06:22:36
【问题描述】:
我开始使用 MEAN STACK,所以我接受了他们的项目(关于发布文章),并且我正在尝试对其进行定制,以便获取我可以使用 angularjs 和 findOne 过滤的所有流的列表按身份证。 我遵循与他们为文章所做的相同的事情来创建与流相关的 JS 文件(流是我的对象)。所以我有一个名为流的集合,我将它导入到 MEAN STACK (db == mean-dev) 使用的同一个数据库中,我尝试了以下代码:
// myApp/serves/models/flow.js
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
// Flow Schema
var FlowSchema = new Schema({
_id: {
type: Number,
default: ''
},
name: {
type: String,
default: '',
trim: true
},
Clients: {
type: Array,
default: '',
trim: true
},
DP Prot: {
type: String,
default: ''
}
/* And 15 others attributes...*/
});
/** Statics */
FlowSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).exec(cb);
};
// Define the collection
mongoose.model('Flow', FlowSchema);
以及控制器代码:
// servers/controllers/flows.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Flow = mongoose.model('Flow'),
_ = require('lodash');
/**
* Find flow by id
*/
exports.flow = function(req, res, next, id) {
Flow.load(id, function(err, flow) {
if (err) return next(err);
if (!flow) return next(new Error('Failed to load flow ' + id));
req.flow = flow;
next();
});
};
/**
* New code count Flows
*/
exports.compte = function(req, res) {
var c;
flow.count({}, function(err, count) {
if (err) return next(err);
c = count;
res.jsonp (count);
});
};
/**
* Show Flow
*/
exports.show = function(req, res) {
res.jsonp(req.flow);
};
/**
* List of Flows
*/
exports.all = function(req, res) {
Flow.find().sort('-name').populate('name', 'application').exec(function(err, flows) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(flows);
}
});
};
我也添加了路由...但是它不起作用,你认为我犯了一些错误吗?提前感谢您的帮助
【问题讨论】:
-
Express 中间件总是采用三个参数:req、res 和 next。您的流程方法中的 ID 为“未定义”。根据您实现路由的方式,您很可能会在 req.params 或 req.body 中找到它。
标签: node.js mongodb express mongoose mean-stack