Hi all,
My code is not passing test case #3 even after refactoring to match the example project. Not sure why It wouldn’t be passing, any guidance would be helpful.
note: Below the app.post to add new exercises to log is my previous iteration where I actually created a new schema and added that to an array of logs. I refactored after realising the example project doesn’t have that functionality but it still doesn’t pass the test cases.
FCC Project link: https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/exercise-tracker
Glitch link: https://glitch.com/edit/#!/fcc-an-exercise-tracker
Code:
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
mongoose.connect(process.env.dburl, { useMongoClient: true }, () =>
console.log("connected to db")
);
var db = mongoose.connection;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static("public"));
// Schema
var userScehma = new mongoose.Schema(
{
username: String,
description: String,
duration: Number,
date: String
},
{ collection: "exercise-tracker-usernames" }
);
// First iteration schema to create a new log entry
/*
var exerciseLog = new mongoose.Schema({
description: String,
duration: Number,
date: String
}, {collection: 'exercise-tracker-usernames'}); */
// Model
var User = mongoose.model("User", userScehma);
// Same for below
// var ExerciseLog = mongoose.model('ExerciseLog', exerciseLog);
// Routes
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
// Display all users
app.get("/api/exercise/users", (req, res) => {
User.find({}, (err, user) => {
res.send(user);
});
});
// Create a new user
app.post("/api/exercise/new-user", (req, res) => {
let newUser = new User({ username: req.body.username });
newUser.save(function(err, user) {
if (err) return console.log(err);
console.log(user);
res.send(user);
});
});
// Add exercises to log - This isn't passing the test case either
app.post("/api/exercise/add", (req, res) => {
let user = req.body.userId;
User.findByIdAndUpdate(
user,
{
description: req.body.description,
duration: req.body.duration,
date: req.body.date || new Date().toISOString().slice(0, 10)
},
{ new: true },
(err, result) => {
res.send(result);
}
);
});
/* Previous iteration to add a new log to an array of logs each entry - this failed the test case
app.post('/api/exercise/add', (req, res) => {
let logEntry = new ExerciseLog({ description: req.body.description,
duration: req.body.duration,
date: req.body.date || new Date().toISOString().slice(0,10) });
let user = req.body.userId;
User.findById(user, (err, user) => {
user.log.push(logEntry);
user.save(err => {if(err) return console.log(err)});
});
// Display the updated user on client
User.findById(user, (err, user) => {
res.send(user);
});
}) */
// Not found middleware
app.use((req, res, next) => {
return next({ status: 404, message: "not found" });
});
// Error Handling middleware
app.use((err, req, res, next) => {
let errCode, errMessage;
if (err.errors) {
// mongoose validation error
errCode = 400; // bad request
const keys = Object.keys(err.errors);
// report the first validation error
errMessage = err.errors[keys[0]].message;
} else {
// generic or custom error
errCode = err.status || 500;
errMessage = err.message || "Internal Server Error";
}
res
.status(errCode)
.type("txt")
.send(errMessage);
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
Thanks for your time