When attempting to store data in mongoDB using mongoose, an error was encountered.
The code snippet from index.js is as follows:
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 executed this file in the node terminal by using - .load index.js
,
and then attempted to save the object "amadeus" with - amadeus.save()
, but received the following error:
Uncaught ReferenceError: amadeus is not defined at REPL16:1:39
Despite the error, the database 'myapp' and the collection 'movies' were successfully created, and can be accessed through the mongo shell.