Tell us what’s happening:
Describe your issue in detail here.
Mongoose has been updated to use async functions and removed callbacks, it’s been quite the struggle to get a grip over the new method since i was accustomed to using callbacks, however
1- my response for the post " is just fine but it won’t accept it even though they’re identical
2- I’m having trouble saving the updated code with the tests as it keeps giving null error but it saves perfectly fine when tested on replit
3- i had to improvise over problem 2 so i queried for the username because it was returned null in the update
4- when i query the database to test on my replit it gives an error for the items like the logs i called to be null "even though it’s been saved "
5- i downgraded mongoose to use callbacks and still doesn’t read the documents
There’s my code
P.S “i added an if statement to check if the doc is null so i could prevent the error but still the tests won’t pass”
`const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
let uri = "mongodb+srv://yasser:" + process.env.PASS + "@cluster0.tuyrtvl.mongodb.net/?retryWrites=true&w=majority"
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
app.use(cors())
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: false }))
let excerciseSchema = new mongoose.Schema({
description: { type: String, required: true },
duration: { type: Number, required: true }
,
date: Date
})
let userSchema = new mongoose.Schema({
username: { type: String, required: true },
log: [excerciseSchema]
});
let Excercise = mongoose.model('Excercise', excerciseSchema);
let User = mongoose.model('User', userSchema);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.post("/api/users", (req, res) => {
let newUser = new User({ username: req.body.username })
newUser.save();
res.send({ 'username': newUser.username, '_id': newUser.id })
})
app.get("/api/users", async (req, res) => {
let list = await User.find({});
console.log(list)
res.send(list)
})
app.post("/api/users/:_id/exercises", async (req, res) => {
let id = req.body[':_id'];
let newExcercise = new Excercise({
description: req.body.description,
duration: req.body.duration,
date: req.body.date
})
if (newExcercise.date === '' || newExcercise.date === 'Invalid Date' || newExcercise.date === null) {
newExcercise.date = new Date().toISOString().substring(0,10);
}
const excercise = await newExcercise.save()
user = await User.findOne({username: {'$regex': 'fcc'}}).then((data)=>{return data.username})
const doc = await User.findByIdAndUpdate(id, {$push: { log: newExcercise } }).then(( update)=>{
let response = {}
response['_id'] = id
response['username'] = user
response['date'] = new Date(newExcercise.date).toDateString()
response['duration'] = parseInt(newExcercise.duration)
response['description'] = newExcercise.description
if(update != null){
update.save()
}
res.json(response)
})
})
app.get("/api/users/:_id/logs", (req,res)=>{
let id = req.params[':_id'];
let logs = User.findById(id,(error,data)=>{
if(error){
console.log(error)
}
else{
if(data != null){
res.send(data)
}
}
})
let counts = User.findById(id,(error,data)=>{
if(error){
console.log(error)
}
else{
if(data != null){
res.send(data.log.length)
}
}
})
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
`
Your project link(s)
solution: boilerplate-project-exercisetracker - Replit
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: