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"
andmongoose
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)
ANDdone(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 nametestdb
) and have still not been able to save. -
using
mongoose.connect(process.env.MONGO_URI)
AND usingmongoose.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
Thanks fCC forum again in advance!