Example does not work, any the server to pass the test

Tell us what’s happening:
The example of the exercise does not work, and when a tried to use the glitch page for this test all the singular tests go out cause time out. I think the server is not working!

Please help, I want to finish it

Your code so far
I can create a user by posting form data username to /api/exercise/new-user and returned will be an object with username and _id. (Test timed out) I can get an array of all users by getting api/exercise/users with the same info as when creating a user. I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added. (Test timed out) I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count). (Test timed out) I can retrieve part of the log of any user by also passing along optional parameters of from & to or limit. (Date format yyyy-mm-dd, limit = int) (Test timed out)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

Hey, could you provide a link to your Glitch project, so we can take a look at your code?

Sure, it is https://glitch.com/edit/#!/steel-lively-selenium?path=server.js%3A31%3A26

In this one I only put the first part, for the search to somebody, if you see when submit is pulsed the system does not work, but I don’t get any error either.

Hey, I think the issue was with your mongoose/mongodb connection. A way to check this is when listening, you can do console.log(mongoose.connection.readyState), and yours came up as 0, which meant disconnected.

I managed to get it to connect by making some changes:

Firstly, you need to update your package versions, change your package.json dependencies to this:

"dependencies": {
		"express": "^4.14.0",
		"mongoose": "5.9.25",
		"mongodb": "3.5.9",
		"body-parser": "^1.15.2",
		"cors": "^2.8.1",
		"shortid": "^2.2.6"
	}

And then open the terminal in Tools > Terminal and run

npm install

I also had issues getting my URI to read from environment variables, so I put the password there instead, like this:


And then created a URI string and added in the password like this:

let uri = 'mongodb+srv://<USERNAME>:' + process.env.PW + '@xxxxxx.xxxxx.mongodb.net/<DATABASE>?retryWrites=true&w=majority'

Finally, I changed the connect method to this updated one:

mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });

You can find this from the freeCodeCamp challenge here:

Finally, I just removed some lines from server.js that might have been interfering, just left with this:

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");

let uri =  'mongodb+srv://<USERNAME>:' + process.env.PW + '@xxxxxx.xxxxx.mongodb.net/<DATABASE>?retryWrites=true&w=majority'
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
});

var schema1 = new mongoose.Schema({
username: {type: String, requeried: true, unique: true}
})
var Person= mongoose.model("Person",schema1);


app.post("/api/exercise/new-user", function(req, res) {
  console.log("Pulsed")
  var username = req.body.username;
  Person.find({ username: username }, (err, data) => {
    console.log("searching")
    if (!data.length) {
      console.log("no encontrado");
      var usuario = new Person({
        username: username
      });
      usuario.save((err, data) => {
        if (err) {
          res.send(err);
        } else {
          res.json(data);
        }
      });
    } else {
      res.send("username already taken");
    }
  });
});

const listener = app.listen(process.env.PORT || 3000, () => {
  console.log("Your app is listening on port " + listener.address().port);
  console.log(mongoose.connection.readyState)
});

After those changes, I was able to connect okay, and got the response back when adding a user!

Hope you are able to get it working, if you’re stuck, have a look at this guide for setting up the database connection:

1 Like