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

Receiving "ReferenceError: mongoose not defined", despite being defined in app.js and installed through npm

Ask Question

I'm building a pretty straightforward tool that will POST the contents of a form to the MongoDB, and will later build a GET request to pull it back out. It started off as following a tutorial and has spun off when I started building a more robust front-end, but I have not been able to get this aspect of the back-end functional.

Below is my code, and you can clearly see mongoose is defined as a variable..

var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');});
app.use(express.static(__dirname + '/'));
var port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var mongoose = require('mongoose');
global.Promise = mongoose.Promise
console.log(console.log(mongoose.connection.readyState));
mongoose.connect("mongodb://localhost:27017/formnote");
var nameSchema = new mongoose.Schema({
 srNumber: String,
 summary: String,
 notePad: String,
var Notes = mongoose.model("Notes", nameSchema);
app.get("/", (req, res) => {
     res.sendFile(__dirname + "/index.html");
app.post("/addnote", (req, res) => {
    var myData = new Notes(req.body);
    myData.save()
        .then(item => {
            res.send("Notes saved to database");
        .catch(err => {
            res.status(400).send("Unable to save to database");
app.listen(port, () => {
    console.log("Server listening on port " + port);

Any help is greatly appreciated..

I was unable to get a trace using console.trace(); any advice for that? I'm still fairly new with Javascript development. – rcreecy Jan 9, 2019 at 21:43

just a shot in the dark. But there are problems when using mongoose global promise in new mongoose version that implementation you have used to connect to your database is obsolete and replaced with this implemenatation

so instead of these lines to connect to the database,

global.Promise = mongoose.Promise
console.log(console.log(mongoose.connection.readyState));
mongoose.connect("mongodb://localhost:27017/formnote");

use this and try

mongoose.connect('mongodb://localhost:27017/formnote',{useNewUrlParser:true})
    .then(function(){
        console.log('mongoDB connected');
    .catch(function(){
        console.log('Error :');
                I had read on one post about that deprecation and had tried removing the promise, and am currently looking into using bluebird as well. I tried that snippet of code into it as well though and it did not resolve. I'm pretty sure its not even getting to the connect phase though due to the ReferenceError. I believe it's a problem with the variable itself.
– rcreecy
                Jan 9, 2019 at 21:45
        

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.