【发布时间】:2013-02-22 08:30:34
【问题描述】:
我遵循 MCV 方法来开发我的应用程序。遇到一个问题,不知道参数是怎么传给回调函数的。
animal.js(模型)
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var animalSchema = new Schema({ name: String, type: String });
animalSchema.statics = {
list: function(cb) {
this.find().exec(cb)
}
}
mongoose.model('Animal', animalSchema)
animals.js(控制器)
var mongoose = require('mongoose')
, Animal = mongoose.model('Animal')
exports.index = function(req, res) {
Animal.list(function(err, animals) {
if (err) return res.render('500')
console.log(animals)
}
}
我的问题来了:为什么模型中的“列表”可以只执行回调而不传递任何参数?错误和动物究竟是从哪里来的?
我想我可能会错过一些与 node.js 和 mongoose 中的回调相关的概念。如果您能提供一些解释或指出一些材料,非常感谢。
【问题讨论】:
标签: javascript node.js callback express mongoose