Hi! I hope you are great.
I have spent the last hours trying to figure out why I get this message in the Exercise Tracker from the Back End Development and APIs Project:
“The date property of any object in the log array that is returned from GET /api/users/:_id/logs should be a string. Use the dateString format of the Date API.”
I already logged the results, and read the different forums related to this topic but I am not able to understand what I am doing wrong. Everything is ok with the date format but don’t know what is happening.
Here is my Replit URL: boilerplate-project-exercisetracker - Nix (beta) Repl - Replit
Here is my index.js code
const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const cors = require('cors')
const { UserModel } = require('./models')
const app = express()
app.use(bodyParser.urlencoded({extended: false}))
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
console.log('mongodb connection success!');
} catch (error) {
console.log(error, "MongoDB Not connected!!")
}
}
connectDB();
require('dotenv').config()
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.post("/api/users", (req, res) => {
let mongoId;
const newUser = new UserModel({
username: req.body.username,
_id: mongoId
})
newUser.save((err, data) => {
if(err) console.log(err)
mongoId = data._id
res.json({
username: req.body.username,
_id: mongoId
})
})
})
app.get('/api/users', async (req, res) => {
const allUsers = await UserModel.find({})
res.send(allUsers)
})
app.post("/api/users/:id/exercises", (req, res) => {
let currentDate = new Date().toDateString()
let date = req.body.date === ""
? currentDate
: new Date(req.body.date).toDateString()
const id = req.params.id
UserModel.findById({
_id: id
}, (err, user) => {
if(err) console.log(err)
user.log.push({
description: req.body.description,
duration: Number(req.body.duration),
date: date
})
user.save((err, updatedUser) => {
if(err) console.log(err)
})
res.json({
_id: id,
username: user.username,
date: date,
duration: Number(req.body.duration),
description: req.body.description
})
})
})
app.get("/api/users/:id/logs", (req, res) => {
const id = req.params.id
UserModel.findById({
_id: id
}, (err, user) => {
if(err) console.log(err)
let count = user.log.length
res.json({
_id: id,
username: user.username,
count: count,
log: user.log
})
})
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
And here is my models.js code
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
from: String,
to: String,
count: Number,
log: [
{
description: String,
duration: Number,
date: String,
_id: false
},
]
})
const UserModel = mongoose.model("UserModel", UserSchema)
module.exports = {UserModel}
Thank you very much in advance for your attention