相关文章推荐
想出国的拐杖  ·  c# - Unity ...·  1 年前    · 
任性的春卷  ·  C++ ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am getting ReferenceError: mongoose is not defined. I am new to node please App.js code is below is as shown below. I am not able to solve it by all the answers provided in google.

   var express = require('express');
 var app = require('mongoose');
 var path = require('path');
 var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser');
 var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.set('views', path.join(__dirname, 'views')); 
   app.set('view engine', 'jade');
 app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
   app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
 app.use('/', index);
 app.use('/users', users);
 app.use(function(req, res, next) {
 var err = new Error('Not Found');
  err.status = 404;
  next(err);
  mongoose.connect('mongodb://localhost:27017/mydb');
  var Schema = new mongoose.Schema({
  cr   : String,
       var user = mongoose.model('myc', Schema);
    app.get('/view', function(req, res){
     user.find({}, function(err, docs){
      if(err) res.json(err);
    else    res.render('index', {users: docs});
      app.use(function(err, req, res, next) {
    res.locals.message = err.message;
     res.locals.error = req.app.get('env') === 'development' ? err : {};
    res.status(err.status || 500);
   res.render('error');
 module.exports = app;

I am using node version6.11.0 and npm v 5.5.1 How to resolve this? pleas help

True and I should have been more clear. Though the answer stands and is obviously accurate, though I was beat by 3 seconds by @Sajeetharan – Hunter Oct 19, 2017 at 18:08

All this answers are in CJ so I would like to show how to connect a MongoDB with ES6 to avert this error:

import mongoose from "mongoose"
const connect = async () => {
    try {
      await mongoose.connect(your DB url);
      console.log("Connected to mongoDB.");
    } catch (error) {
      throw error;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.