Error: Attempted to save data with mongoose but encountered an Uncaught ReferenceError; `amadeus` is not defined at line 16,

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.

Answer №1

To address the issue mentioned above, one potential solution is to execute the index.js file by using the command

node -i -e "$(< index.js)"
in the system terminal instead of the node shell. This error could be stemming from changes in the latest Node.js version.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating through an array for drawing a polyline on Google Maps

Is there a way to efficiently loop through an array of coordinates in order to set markers and draw a line on Google Maps using the path property? Please see the example below for clarification: const lineSymbol = { path: google.maps.SymbolPath.FORWARD_ ...

Ways to divide a string into pairs of two using jQuery

I have a variable containing a string with exactly 44 characters: 02081516171821242936374750565865666871737476 I want to split it into an array in the following format: arr[0]=02 arr[1]=08 . . arr[21]=76 Can someone help me achieve this? Thank you. UP ...

What are the potential disadvantages of relocating the login logic from the 'signIn()' function in NextAuth.js?

My experience with NextAuth.js for the first time has led me to realize that signing in using the Credentials provider can be a bit tricky when it comes to error handling. It seems like the default implementation should resemble something along these lines ...

Executing a file upload using ng-click="upload('files')" within Selenium Webdriver

Is it possible to automate a file upload when the HTML code does not include an < input type='file' > instead, uses a link <a ng-click="upload('files')"> File Upload </a> Clicking this link opens a file selector f ...

Update the image links to automatically refresh every half a second based on the values inputted in the text bars

Is there a better and simpler way to define AAAAAAAAAA, BBBBBBBBBB, and CCCCCCCCCC using the links provided in the text bars named chart1, chart2, and chart3? This learning project is being done through Notepad++ for personal use only, with no need to sa ...

What is the method to retrieve the string value from a JavaScript String object?

Is there a way to extend a method to the String prototype and manipulate the string value? I'm facing some difficulty in accessing the actual string value, as this, the current object context, seems to refer to the string object instead. String.pro ...

The AngularJS $http.post method mimicking a successful $.ajax request is causing a 401 UNAUTHORISED error

I have been working on rewriting a functional $.ajax server call to utilize AngularJS $http.put. However, the $http method returns a 401 unauthorized error. The original ajax call I am attempting to rewrite is structured like this: $.ajax({ url: domain ...

Encountering Axios errors while executing API calls at a high frequency

Recently, I have been facing some challenges with making API calls from localhost using axios in a loop. While it works smoothly at times, most often I encounter errors like: cause: Error: connect ECONNREFUSED ::1:8000 at TCPConnectWrap.afterConnect ...

Trying to incorporate a PHP echo statement into the innerHTML of a button is ineffective

I have a piece of jQuery code that is not functioning as expected: $("#erase").click(function(){ $("#erase").attr("disabled", "disabled"); // Prevent double-clicking $(this).html("Erasing..."); $.post("erase.php", {target: $("#targ ...

Using JavaScript for Text Processing on Disk

Currently, I have a set of HTML files that require automated processing such as regex replacements and more complex actions like copying specific text blocks from one file to another. I am considering creating a series of scripts to handle this processing ...

Using Commas to Separate ChartJS Data Points

I am having trouble formatting numbers in a ChartJS graph to include commas. For example, my data points are currently displayed as 1032.05, 4334.75, 8482.46 but I need them to show as 1,032.05, 4,334.75, 8,482.46. If you would like to see the current cod ...

Issues with React Native: Struggling to iterate through an array and show it with an image URL stored

Just diving into React and I'm facing a challenge with my code. I have an array stored in my .state that I want to loop through and display the images. (I'm using 'Image' and a Card object from https://github.com/lhandel/react-native-ca ...

Embracing ES6 Imports Over Requires in Node.js

var MCrypt = import('mcrypt').MCrypt; What is the experience of utilizing import instead of require for the above code? It seems challenging to achieve in a single line. ...

Creating a masterpiece on the final piece of paper

As someone who is new to programming with JavaScript and canvas, I have a function called drawLines(canvasIndex, startPosition) that is used to draw lines on a canvas. The function takes in two arguments: canvasIndex, which represents the canvas being draw ...

"Seeking guidance on incorporating LIKE, NOT IN, and IN search capabilities using CakePHP and MongoDB. How can this be

I am currently using the ichikaway cake-php MongoDB plugin to establish a connection between my CakePHP application and MongoDB. I am encountering an issue when calling the find method, as shown in the code snippet below: debug($this->Keyword->find( ...

What are some effective methods for troubleshooting npm modules?

Typically, the process involves installing React with yarn/npm install react and then using it by importing React from 'react' Imagine you need to debug a React source code, so you clone a GitHub repository. But how do you incorporate this sour ...

"Enhance Your Phonegap Android App with Pinch Zoom Capability

I am currently working on an Android Application with the help of jQuery Mobile and Phonegap. My goal is to implement pinch zoom functionality on the App pages. I have come across several suggestions that involve using the following line, but unfortunatel ...

The property 'type' is not found on the 'IntrinsicAttributes & DraggableProps' type in NEXTJS/React

Utilizing @hello-pangea/dnd (a fork of react-beautiful-dnd) to construct a drag and drop list. <Draggable draggableId={section.id} index={index} type="section"> //type <- new/ custom prop However, the 'type' does not exist in ...

Ways to conceal and reveal a different section at the same time (like an accordion)

Looking for guidance on implementing an accordion-style functionality, where clicking a navigation link hides one section and reveals another seamlessly with automatic scrolling to the start of the section (end of the navigation). Any tips or starting poin ...

How to Update Data in MongoDB with Node-Red

Just starting out with Node-Red and MongoDB. I'm trying to update a document in a collection using Node-Red. Here's the code I have in a Function node: var newMsg = msg; newMsg.operation = 'findAndModify'; newMsg.payload = { ...