Search code examples
javascriptdatabasemongodbmongoosemongoose-schema

Uncaught ReferenceError: amadeus is not defined at REPL16:1:39 while saving data using mongoose


Encountered an error while trying to save data in mongoDB using mongoose.

index.js file -

const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/myapp')
    .then(() => {
        console.log("Connection established!")
    })
    .catch((e) => {
        console.log("Error!")
        console.log(e)
    })
    
const movieSchema = new mongoose.Schema({
    title: String,
    year: Number,
    score: Number,
    rating: String
})

const Movie = mongoose.model('Movie', movieSchema)
const amadeus = new Movie({ title: 'Amadeus', year: 1984, score: 8.4, rating: 'PG'})

I loaded this file from node terminal using - .load index.js and then tried to save amadeus using - amadeus.save() but it gave this error:

Uncaught ReferenceError: amadeus is not defined at REPL16:1:39

But the 'myapp' db and 'movies' collection was created and I'm able to access it from mongo shell.


Solution

  • One solution to the above problem is to run the index.js file using - node -i -e "$(< index.js)" inside the system terminal and not inside the node shell. The reason of this error is maybe related to the new version of Node.js