Tell us what’s happening:
I have tried to solve what the task is. I even compared with the solution page. But I could not get the JSON data to be stored in the MongoDB. There seems to be some sort of connection error. I had my myApp.js run smoothly and only throw an error, when the tests on freeCodeCamp were initiated.
I now have bluntly copied over the solution from the solution page. But it still does not work. I am stuck. I must oversee something simple. But I am probably too close. I have started watching YouTube-tutorials about MongoDB and Mongoose. But so far they all started, where the connection already was there. I could not resolve my problem. Can you help?
Your code so far
https://repl.it/join/xgzbxgas-mseibert
.env
MONGO_URI='mongodb://localhost/my_database'
myApp.js
/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
name: String,
age: Number,
favoriteFoods: [String]
});
/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);
var createAndSavePerson = function(done) {
var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["eggs", "fish", "fresh fruit"]});
janeFonda.save(function(err, data) {
if (err) return console.error(err);
done(null, data)
});
};
const createManyPeople = (arrayOfPeople, done) => {
done(null /*, data*/);
};
const findPeopleByName = (personName, done) => {
done(null /*, data*/);
};
const findOneByFood = (food, done) => {
done(null /*, data*/);
};
const findPersonById = (personId, done) => {
done(null /*, data*/);
};
const findEditThenSave = (personId, done) => {
const foodToAdd = "hamburger";
done(null /*, data*/);
};
const findAndUpdate = (personName, done) => {
const ageToSet = 20;
done(null /*, data*/);
};
const removeById = (personId, done) => {
done(null /*, data*/);
};
const removeManyPeople = (done) => {
const nameToRemove = "Mary";
done(null /*, data*/);
};
const queryChain = (done) => {
const foodToSearch = "burrito";
done(null /*, data*/);
};
/** **Well Done !!**
/* You completed these challenges, let's go celebrate !
*/
//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------
exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;
exports.findPeopleByName = findPeopleByName;
exports.findOneByFood = findOneByFood;
exports.findPersonById = findPersonById;
exports.findEditThenSave = findEditThenSave;
exports.findAndUpdate = findAndUpdate;
exports.createManyPeople = createManyPeople;
exports.removeById = removeById;
exports.removeManyPeople = removeManyPeople;
exports.queryChain = queryChain;
The error I get:
node:130) Warning: Accessing non-existent property ‘MongoError’ of module exports inside circular dependency
(Usenode --trace-warnings ...
to show where the warning was created)
(node:130) 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:130) 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.
Your app is listening on port 3000
(node:130) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
name: ‘MongoNetworkError’
}]
at Pool. (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:315:20)
at /home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/pool.js:562:14
at /home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/pool.js:995:11
at /home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connect.js:32:7
at callback (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connect.js:280:5)
at Socket. (/home/runner/boilerplate-mongomongoose/node_modules/mongodb/lib/core/connection/connect.js:310:7)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:130) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict
(see Command-line API | Node.js v21.2.0 Documentation). (rejection id: 1)
(node:130) [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.
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13597.94.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.186 Safari/537.36
.
Challenge: Create and Save a Record of a Model
Link to the challenge: