最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.JSExpressMongodbMongoose Simple Connection Issue - Stack Overflow

programmeradmin2浏览0评论

Hi There: I'm new to Node/Mongo/Express, etc and trying to get a simple connection to a mongodb through mongoose. At this point I'm just trying to establish a connection but get the error:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
ReferenceError: mongoose is not defined
    at Object.<anonymous> (/Users/mattydorey/Code/napkin_0.1/testdb.js:8:1)
    at Module._pile (module.js:407:26)
    at Object..js (module.js:413:10)
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at Array.<anonymous> (module.js:426:10)
    at EventEmitter._tickCallback (node.js:126:26)

Any help is much appreciated. Here is my code:

// Launch express and server
var express = require('express');
var app = express.createServer();

//connect to DB
var moongoose = require('/Users/me/node_modules/mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/napkin_0.1');

//Configure Node w/ Dependencies
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(require("stylus").middleware({
      src: __dirname + "/public",
      dest: __dirname + "/public",
      press: false
    }));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  //app.use(require('stylus').middleware({ src: __dirname + '/public' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

// Define Model
mongoose.model('User', {
  collection: 'user',
  properties: [
    'created',
    'username',
    'password',
    'email'
  ],
  indexes: [
    'created',
    [{username: 1},{unique: true}],
    [{email: 1},{unique: true}]
  ],
  static: {},
  methods: {},
  setters: {},
  getters: {}
  }
);

//Define Collection
var User = db.model('User');

// Handle Data
var user = new User();
user.created = new Date();
user.username = "TEST";
user.password = "PASS";
user.email = "someemail";
user.save();

//Launch Server
app.listen(3002);

Hi There: I'm new to Node/Mongo/Express, etc and trying to get a simple connection to a mongodb through mongoose. At this point I'm just trying to establish a connection but get the error:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
ReferenceError: mongoose is not defined
    at Object.<anonymous> (/Users/mattydorey/Code/napkin_0.1/testdb.js:8:1)
    at Module._pile (module.js:407:26)
    at Object..js (module.js:413:10)
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at Array.<anonymous> (module.js:426:10)
    at EventEmitter._tickCallback (node.js:126:26)

Any help is much appreciated. Here is my code:

// Launch express and server
var express = require('express');
var app = express.createServer();

//connect to DB
var moongoose = require('/Users/me/node_modules/mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/napkin_0.1');

//Configure Node w/ Dependencies
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(require("stylus").middleware({
      src: __dirname + "/public",
      dest: __dirname + "/public",
      press: false
    }));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  //app.use(require('stylus').middleware({ src: __dirname + '/public' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

// Define Model
mongoose.model('User', {
  collection: 'user',
  properties: [
    'created',
    'username',
    'password',
    'email'
  ],
  indexes: [
    'created',
    [{username: 1},{unique: true}],
    [{email: 1},{unique: true}]
  ],
  static: {},
  methods: {},
  setters: {},
  getters: {}
  }
);

//Define Collection
var User = db.model('User');

// Handle Data
var user = new User();
user.created = new Date();
user.username = "TEST";
user.password = "PASS";
user.email = "someemail";
user.save();

//Launch Server
app.listen(3002);
Share Improve this question edited Jun 13, 2011 at 17:34 MPelletier 16.7k18 gold badges89 silver badges140 bronze badges asked Jun 12, 2011 at 8:49 MattyMatty 933 silver badges6 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 8

And check the require path, try just :

var mongoose = require('mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/napkin_0.1');

Try this

const mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/napkin_0.1')
发布评论

评论列表(0)

  1. 暂无评论