节点js项目中出现意外的标记变量

时间:2016-09-27 11:50:04

标签: node.js express mean-stack

在我的节点js项目中添加模型后出现以下错误,我完全不知道,请有人帮助我。

     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)

我的模特,

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

  });

};

在我的节点js项目中添加模型后出现以下错误,我完全不知道,请有人帮助我。

1 个答案:

答案 0 :(得分:2)

错误在于:

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

您必须将,替换为;,或者只是删除var

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

我没有找到错误消息显示的确切行,但是代码中的错误与上面相同。