Invalid Schema MongoDB Error

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:

Hello there,

It would be more useful to see your live project, but here are some things you can try:

  1. console.log your MONGO_URI to ensure it is presented correctly. Ensure you have all the correct parameters filled in the URI. If undefined is logged, install the dotenv package, and require it thusly: `require(‘dotenv’).config()
  2. Comment out the saving of a person, and see if you get anything by using the find method on your Person model, just to confirm if there is in fact a database connection issue.

The error seems to suggest the issue is with your URI. So, spend time debugging that.

Hope this helps

Thank you for the feedback I found the problem. I had been running into issues from the previous module with Glitch so I started using Repl.It but because I liked the ‘show’ feature in Glitch where I could see the output of a given URI as I was coding it I have been coding in both simultaneously and ran into a copy and paste issue when I was copying the .env file from Repl.It. Glitch has you manually enter the key/value separately as part of the UI whereas Repl.It just has you enter the whole thing manually. I ended up entering the key in both the key entry and as part of the value “key=value”.