简体   繁体   English

语法错误:意外令牌{

[英]Syntax Error: unexpected Token {

Here is my Model: 这是我的模特:

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);
  });
};

Here is the Route: 这是路线:

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);
              }
          });
        }
  };
});

At line no 9 in Route.js (Here in above code line no.4) I get an error: 在Route.js的第9行(上面的代码第4行),我得到一个错误:

  Party.getAllParties(err, parties){

Syntax error: {unexpected token 语法错误:{意外令牌

Why is it unexpected? 为什么会出乎意料? Can't I use a function's body here??? 我不能在这里使用函数的主体吗???

You need to pass in a function instead. 您需要改为传递一个函数。 A block statement like that outside unfortunately won't work. 不幸的是,在外面这样的阻止声明是行不通的。

This is most likely what you need: 这很可能是您需要的:

Party.getAllParties(function (err, parties) {  
    // rest of your logic here
});

You can't place a block statement these when you're calling a function. 调用函数时,不能放置这些语句。

It looks like you want something like 好像你想要的东西

Party.getAllParties(function() {
    // ...
})

Where you pass an anonomous callback function 您在哪里传递匿名回调函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM