Javascript 语法错误:意外的令牌{
这是我的模型:Javascript 语法错误:意外的令牌{,javascript,node.js,mongoose,routes,Javascript,Node.js,Mongoose,Routes,这是我的模型: 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 =
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() {
// ...
})
当您传递一个匿名回调函数时,您不能在调用函数时放置block语句 看起来你想要像这样的东西
Party.getAllParties(function() {
// ...
})
当你传递一个匿名回调函数时,你需要传入一个函数。不幸的是,像这样的块语句在外面是不起作用的 这很可能是您需要的:
Party.getAllParties(function (err, parties) {
// rest of your logic here
});
您需要传入一个函数。不幸的是,像这样的块语句在外部是不起作用的 这很可能是您需要的:
Party.getAllParties(function (err, parties) {
// rest of your logic here
});
我曾尝试按您所说的那样执行,但在浏览器中我收到一个错误,说明未定义参与方。在命令提示中我也收到了错误:未处理的拒绝错误:发送邮件后无法设置邮件头。我已尝试按您所说的那样执行,但在浏览器中我收到一个错误,说明未定义参与方。在命令提示中也是如此我得到错误:未处理的拒绝错误:发送后无法设置标题。我尝试按您所说的执行,但在浏览器中我得到一个错误,表示未定义参与方。以及在命令提示符中,我得到错误:未处理的拒绝错误:发送后无法设置标题。我尝试按您所说的执行,但在浏览器中我收到一个错误,说明未定义参与方。在命令提示符下,我也收到错误:未处理的拒绝错误:发送邮件后无法设置邮件头。