Vishal Vishal - 8 months ago 51
Node.js Question

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

//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(){

At line no 9 in Route.js (Here in above code line no.4) I get an error:

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