简体   繁体   中英

Node.JS / Express / Mongodb/Mongoose Simple Connection Issue

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._compile (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",
      compress: 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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