nlr_p nlr_p - 2 months ago 9
Node.js Question

Unexpected token var in node js project

I got the following error after adding model in my node js project and i am completely not aware of it,can anyone help me please.

uncaughtException: Unexpected token var date=Tue Sep 27 2016 17:14:56 GMT+0530 (IST), pid=27090, uid=1000, gid=1000, cwd=/var/www/html/meanjs, execPath=/usr/bin/nodejs, version=v4.5.0, argv=[/usr/bin/nodejs, /var/www/html/meanjs/server.js], rss=66248704, heapTotal=41467744, heapUsed=25796056, loadavg=[1.15185546875, 0.77734375, 0.73486328125], uptime=27407
SyntaxError: Unexpected token var
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at /var/www/html/meanjs/config/lib/mongoose.js:15:5
at Array.forEach (native)
at Object.module.exports.loadModels (/var/www/html/meanjs/config/lib/mongoose.js:14:30)
at Object.<anonymous> (/var/www/html/meanjs/config/lib/app.js:20:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/www/html/meanjs/server.js:6:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)


My model,

'use strict';

/**
* Module dependencies
*/
var mongoose = require('mongoose'),
path = require('path'),
config = require(path.resolve('./config/config')),
Schema = mongoose.Schema,
var CategorySchema = new Schema({
categoryname:String


});
/**
* Hook a pre validate method to test the local password
*/
mongoose.model('categories', CategorySchema);


app.js

'use strict';

/**
* Module dependencies.
*/
var config = require('../config'),
mongoose = require('./mongoose'),
express = require('./express'),
chalk = require('chalk'),
seed = require('./seed');

function seedDB() {
if (config.seedDB && config.seedDB.seed) {
console.log(chalk.bold.red('Warning: Database seeding is turned on'));
seed.start();
}
}

// Initialize Models
mongoose.loadModels(seedDB);

module.exports.init = function init(callback) {
mongoose.connect(function (db) {
// Initialize express
var app = express.init(db);
if (callback) callback(app, db, config);

});
};

module.exports.start = function start(callback) {
var _this = this;

_this.init(function (app, db, config) {

// Start the app by listening on <port> at <host>
app.listen(config.port, config.host, function () {
// Create server URL
var server = (process.env.NODE_ENV === 'secure' ? 'https://' : 'http://') + config.host + ':' + config.port;
// Logging initialization
console.log('--');
console.log(chalk.green(config.app.title));
console.log();
console.log(chalk.green('Environment: ' + process.env.NODE_ENV));
console.log(chalk.green('Server: ' + server));
console.log(chalk.green('Database: ' + config.db.uri));
console.log(chalk.green('App version: ' + config.meanjs.version));
if (config.meanjs['meanjs-version'])
console.log(chalk.green('MEAN.JS version: ' + config.meanjs['meanjs-version']));
console.log('--');

if (callback) callback(app, db, config);
});

});

};


I got the following error after adding model in my node js project and i am completely not aware of it,can anyone help me please.

Answer

The error lies here:

Schema = mongoose.Schema,
var CategorySchema = new Schema({
  categoryname:String
});

You have to replace the , with a ;, or just get rid of the var.

Schema = mongoose.Schema,
CategorySchema = new Schema({
  categoryname:String
});

I didn't found the exact line as the error message shows, but it will be the same error in your code as above.

Comments