Create and save model working on neither Repl.it nor Glitch

On the “Create and Save a Record of a Model” section of MongoDB/Mongoose in the curriculum.

I tried changing my code a bunch with no luck and then even tried copy-pasting the solution verbatim and I have had no luck. Have tried on Repl.it and on Glitch, seems like Glitch is the better one but no luck either way.

Here’s my Glitch, and here’s the more specific code section in question:

Code

var mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });


const personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  favoriteFoods: [String]
});

var Person = mongoose.model('Person', personSchema);

const createAndSavePerson = function(done) {
  var lucas = new Person({name: "Lucas", 
                          age: 19, 
                          favoriteFoods: ["avocado toast", "dark chocolate"]
                         });
  lucas.save(function(err, data) {
      if(err) return console.error(err);
      done(null, data);    
  });
};

I swapped my for “testdb” and passed the first two challenges so I assume everything is working with that. A few things I’ve tried to troubleshoot with:

  • reverted to earlier dependencies in hopes of not breaking things, based on the answer from before; mongodb as "^3.0.0" versus "^3.6.4" and mongoose as "^5.6.5" versus "^5.11.15" (although based on how I understand versioning, these will both just update to the most recent because they are not major releases.

  • trying to return console.error(err) AND done(err) (from this forum post and the fCC solution)

  • I saw the recommendations in this post, and while I have concurrently restarted the challenges multiple times, I have not had any luck. I created a collection (dbname testdb and collection name testdb) and have still not been able to save.

  • using mongoose.connect(process.env.MONGO_URI) AND using mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology:true }). The former was suggested in this forum post but when I ran the former on my Repl.it, I got essentially told to put both back, like this (sorry for all of the dropdowns, kinda giddy about advanced discobot tutorial lol):

URL Parsing Error
'(node:50) 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.
Your app is listening on port 3000
(node:50) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.'

Both my Glitch and my Repl.it pass the first two lessons (“Install and Set Up Mongoose” and “Create a Model”) and both fail on “Create and Save a Record of a Model.” As such, I don’t think it’s a database issue.

I’m can’t tell what’s going on on my Glitch and am not seeing stuff in the Glitch command line but on Repl.it, here’s the error I’m getting:

Repl Error

(node:50) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-02.hghqz.mongodb.net:27017] on first connect [MongoNetworkTimeoutError: connection 4 to cluster0-shard-00-02.hghqz.mongodb.net:27017 timed out] at Pool.<anonymous> (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:438:11) at Pool.emit (events.js:198:13) at createConnection (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:562:14) at connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1009:9) at callback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:75:5) at conn.command (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:101:9) at _callback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:328:7) at Connection.errorHandler (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:343:7) at Object.onceWrapper (events.js:286:20) at Connection.emit (events.js:198:13)

Let me know if you have any ideas here! Feels like I’ve tried everything and really just cannot pass more of these tests :frowning:

Thanks fCC forum again in advance!

Hey again,

I was able to pass by:

  1. Forking your Repl project
  2. Adding a .env file with my DB URI
  3. RUNning and submitting the project

That is all.

So, I suspect the issue is with the permissions on your database. Did you follow this step:

Add your IP address

  • If you now click on the green Get Started button in the bottom left of your screen, you should see the next step to take highlighted, Add your IP address , click on it.
  • Follow the instructions by clicking on the IP Access List tab under the >Security tab.
  • Click on the green ADD IP ADDRESS button.
  • In the modal, click the ALLOW ACCESS FROM ANYWHERE button and you should see 0.0.0.0/0 pre-filled for the whitelist entry field, click the green Confirm button.

Otherwise, excellent post! It is not often Campers take the time to be as thorough as you have.


True.

Hope this helps

Thank you for the quick response!

Yes, I whitelisted my IP! Also, those instructions maybe could be tweaked—it’s now Security, then Network Access, and then the ability to allow IPs.

Maybe it’s how I’m accessing my database? Screen Shot 2021-02-04 at 9.21.53 PM

I’m using testdb.testdb as the <dbname> bit—is that correct?

Is there a way for me to test access to the database some other way? Or to get a link directly of this collection? I remember copy-pasting that link from before but looks like no luck right now. Had assumed db wasn’t the issue because it passed the first challenge which seemed to require some database connection.

I am not sure what you mean by this. You access the database by mongoose.connect(MONGO_URI). Then you send queries to the database through methods like Person.save(). So, the way you access your database, and the way I access mine, in this challenge’s case, were identical.


One thing I am remembering is some devs have had issues with the name and password they chose for their account. That is, they had ‘special characters’ in their username/password, and this caused connection issues.

It might help if you started by changing your password.

Hope this helps

I just passed it thank you so much for your help! I had not had the database in quotes I think, and referencing as testdb.testdb (database.collection) was not right; the node shell flagged me for using a “.” in the db name.

Thanks again!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.