MongoDB Challenge 3 - Create and Save a Model - Problem Passing the Test

Tell us what’s happening:
I have passed all of the previous tests just fine, but I can’t seem to pass this one. I tried everything, and eventually even tried copy-pasting the official solution on this forum, and even that doesn’t work.

In the code below, I’ve placed the console.log(“hmm”) and console.log(data) on purpose, and neither those, nor the console.error(err) error logs ever run. All I ever get is the GET request made by freeCodeCamp on my terminal, and after a while, the following timeout error on the freeCodeCamp challenge page:

// running tests
Creating and saving a db item should succeed (Test timed out)
// tests completed

The link to my project is balonur.glitch.me

Your code so far

const mongoose = require("mongoose");

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

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

const Person = mongoose.model("Person", personSchema);

const createAndSavePerson = function(done) {
  const danteBal = new Person({name: "Dubunta Bal", age: 2, favoriteFoods: ["Salmon", "Cheese", "Yoghurt"]});
  
  danteBal.save(function(err, data) {
    console.log("hmm");
    if (err) return console.error(err);
    console.log(data)
    done(null, data);
  });
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Create and Save a Record of a Model

Link to the challenge:

Hello there,

When I put your project link into the challenge tester, I get this output in the console:
image

A status 500 error has to do with the server - in this case, your Glitch project.

So, could you have a look in the Glitch logs for any errors or useful output?

The strange thing is I’m not getting anything at all, neither in the logs, nor in the terminal. All I get is the GET request once I run the tests, and nothing but.

I managed to get an error by running

node server.js

in the terminal, trying to manually launch the server:

$ node server.js
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1270:14)
    at listenInCluster (net.js:1318:12)
    at Server.listen (net.js:1405:7)
    at Function.listen (/rbd/pnpm-volume/dc72d3c0-1652-4885-9c4f-6b01872527fb/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/app/server.js:344:20)
    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)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1297:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Some things to note:

  • With Glitch, you should not have a need to start/restart the server through the terminals.
  • If you do, choosing the correct ports becomes important.
  • Many campers have had issues using Glitch for the projects, just due to stability reasons, as well as the specific way Glitch handles the virtual servers.

So, I suggest you use the link in the README.md file to create a new project in Repl.it, and copy-pasta the code over.

Hope this helps

Hi Sky020, I tried the link for cloning it the README.md file, but it seems to be the link for cloning the template for the Node Express section.

FCC Mongo & Mongoose Challenges
===============================
[![Run on Repl.it](https://repl.it/badge/github/freeCodeCamp/boilerplate-express)](https://repl.it/github/freeCodeCamp/boilerplate-express)

Do you know of an easy way of transferring from Glitch to Repl.it, if such a template link is not available?

Well, you could fork the boilerplate directly from GitHub to Repl.it.

Hi there Sky,

I discovered that the problem stemmed from the

.env

file. While I created one for myself, only containing the MONGO_URI key and its value, I noticed that when I access process.env from the the myApp.js file, it was accessing a totally different one, with many different keys, except for the one I created.

To solve this issue, I unfortunately had to forego the usage of the .env file entirely and just wrote my credentials inside the myApp.js file.

I have a feeling that maybe there is an invisible .env file lurking somewhere, which I can’t see? If this isn’t just an issue on my end, it could be worthwhile to look into these, so others do not get confused either.

Anyhow, thank you for your quick responses, really appreciate your support.

1 Like