语法错误:意外的令牌{
这是我的模型:
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行(这里在上面的代码行no.4)我得到一个错误:
Party.getAllParties(err, parties){
语法错误:{意外的令牌
为什么是意外? 我不能在这里使用一个function的身体?
你需要传递一个函数。 不幸的是,这样的块语句不起作用。
这很可能是你需要的:
Party.getAllParties(function (err, parties) { // rest of your logic here });
当你调用一个函数时,你不能放置一个块语句。
它看起来像你想要的东西
Party.getAllParties(function() { // ... })
你传递了一个自主的callback函数