I was able to pass the last lesson ‘Create a Model’ which already had the code block where I am connecting to MongoDB but now on the next lesson ‘Create and Save a Record of a Model’ that same line of code is throwing an error. See error from the log below. I’m wondering if the issue might be that myApp.js is trying to add a record to a database that does not exist but I didn’t see any steps that talked about explicitly creating a new database and instead figured that myApp.js was creating the database dynamically when creating the schema. In the part of the MONGO_URI connection string I just plugged in a random ‘PersonDB’ value but maybe this is the problem?
Log Details:
(node:8549) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:8549) UnhandledPromiseRejectionWarning: Error: Invalid schema, expected mongodb
or mongodb+srv
at module.exports (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongodb/3.5.9/node_modules/mongodb/lib/url_parser.js:22:21)
at deprecated (internal/util.js:66:15)
at connect (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongodb/3.5.9/node_modules/mongodb/lib/operations/connect.js:277:3)
at cb (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongodb/3.5.9/node_modules/mongodb/lib/mongo_client.js:222:5)
at maybePromise (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongodb/3.5.9/node_modules/mongodb/lib/utils.js:719:3)
at MongoClient.connect (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongodb/3.5.9/node_modules/mongodb/lib/mongo_client.js:218:10)
at Promise (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongoose/5.9.21/node_modules/mongoose/lib/connection.js:716:12)
at new Promise ()
at NativeConnection.Connection.openUri (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongoose/5.9.21/node_modules/mongoose/lib/connection.js:709:19)
at Mongoose.connect (/rbd/pnpm-volume/7336be79-5d7b-45da-ae7d-53235816afaf/node_modules/.registry.npmjs.org/mongoose/5.9.21/node_modules/mongoose/lib/index.js:335:15)
at Object. (/app/myApp.js:6:9)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
(node:8549) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:8549) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
var app = express();
var mongodb = require('mongoose');
mongodb.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const Schema = mongodb.Schema;
const personSchema = new Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongodb.model("Person", personSchema);
var createAndSavePerson = function(done) {
var newPerson = new Person({name: "bob smith", age: 6, favoriteFoods: ["pizza", "beer", "Chipotle"]});
newPerson.save(function(err, data){
if(err) return console.error(err);
done(null, data)
});
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
.
Challenge: Create and Save a Record of a Model
Link to the challenge: