这是我的模特:
var mongoose = require('mongoose');
var partySchema = new mongoose.Schema({
partyCode: Number,
partyName: String,
mobileNo: String
});
var Party = module.exports = mongoose.model('Party', partySchema);
module.exports.getAllParties = function(callback){
Party.find().lean().exec(function(err, parties){
if (err) return callback(err, null);
callback(null, parties);
});
};
这是路线:
router.get('/', function(req, res, next){
//retrieve all parties from Party model
//mongoose.model('Party').find({}, function (err, parties) {
Party.getAllParties(err, parties){
if (err) {
return console.error(err);
} else {
//respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
res.format({
//response in dust or jade files
html: function(){
res.render('Party', {
title: 'Party',
"parties" : parties
});
},
//JSON response will show all parties in JSON format
json: function(){
res.json(parties);
}
});
}
};
});
在Route.js的第9行(上面的代码第4行),我得到一个错误:
Party.getAllParties(err, parties){
语法错误:{意外令牌
为什么会出乎意料?我不能在这里使用函数的主体吗???
您需要改为传递一个函数。不幸的是,在外面这样的大块声明是行不通的。
这很可能是您需要的:
Party.getAllParties(function (err, parties) {
// rest of your logic here
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句