Cannot pass the test2 to post the api with new username

Tell us what’s happening:
i just started doing this challenge. my mongodb is connected but I cannot see the username in my mongodb cluster collections. Hence I guess I cannot pass the test 2 to post the new user on the api. please let me know where I went wrong here so that i can proceed with other tests in this challenge. Looks like a mongo db connection issue but how is my db connection active yet not showing the collwction with username in mongo db cluster.
Your code so far
my code until test 2 to test the api with new username:

const express = require('express');

const mongoose = require('mongoose');
const mongo = require('mongodb');
const app = express();
const cors = require('cors')
const dotenv = require('dotenv');
dotenv.config();

const port = process.env.PORT || 3000;

const mongoString = `mongodb+srv://${process.env.DB_USERNAME}:${
	process.env.DB_PASSWORD
}@backendlessons.uoknb.mongodb.net/db1?retryWrites=true&w=majority`;

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

mongoose.connection.on('error', function(error) {
	console.log(error);
});
mongoose.connection.on('open', function() {
	console.log('Connected to MongoDB database.');
});


app.use(cors());
app.use(express.static('public'))
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
});


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

let excerciseSessionSchema = new mongoose.Schema({description:{type:String, required:true},
  duration:{type:Number, required:true},
  date:String
});

let userSchema = new mongoose.Schema({username:{type:String, required:true}, log:[excerciseSessionSchema]
});

let Session = mongoose.model('Session', excerciseSessionSchema);

let User = mongoose.model('User', userSchema);

let bodyParser = require('body-parser');
app.post('/api/excercise/new-user', bodyParser.urlencoded({extended:false}),  (request, response) =>{
  let newUser = new User({username:request.body.username});
  newUser.save((error, savedUser)=>{
    if(!error){
      let responseObject={};
     responseObject['username']
      = savedUser.username;
      responseObject['_id']
    = savedUser.id;
    response.json(responseObject);
    }
  })
})

my repl.it link is here:
https://repl.it/@meeramenon07/boilerplate-project-exercisetracker#README.md

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; SM-A107F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.111 Mobile Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

Hello there,

I think, to help you debug, log the result (if any) of the error:

newUser.save((error, savedUser)=>{
    if(!error){
      let responseObject={};
      responseObject['username'] = savedUser.username;
      responseObject['_id'] = savedUser.id;
      response.json(responseObject);
    }
  })

Also, maybe look at the docs for the .save method: Mongoose v5.11.19: API docs (mongoosejs.com)
specifically, the return value

Hope this helps

ok will try and update .tq

the console.log error message:
‘connected to mongodb
listening to events in the db class has been depracated and will be removed in the next major version.’

I did until test 4 and able to get required result on testing the api …am able to get user name, user id. new session date, duration and description etc on testing my api…however i am not passing the tests 2 , 3 and 4. Have done until test 4. Iam proceeding to do the remaining test and again check result on fcc if I am passing all tests. But Its surprising that I am getting required outcome in mongodb and replit and able to test my api successfully but not passing my tests due to some bug in mongodb apparently. In my replit console there is no error message. Just a message saying "depracated events in mongodb class "
can someone advise further?Is anyone else facing same situation?
even though the depracating error showscin my console I finally passed all the tests in this challenge. thanks to all who advised .

Would you mind sharing a link to your project and code?

Hey there,

maybe you want to have a look at the actual tests of the projects to understand what they test for.

1 Like

This is my project code link at repl.it :
https://replit.com/@meeramenon07/boilerplate-project-exercisetracker#server.js

Thank you, for linking that.

When I try to run the tests on your project I get CORS errors:

I see you have cors, but do not use it:

const cors = require('cors');

I have included const cors = require(‘cors’); then why it is not being used I wonder?

For the same reason I can declare a variable myVariable, and it never be used in a program:

const myVariable = 2;

function main() {
  let a = 10;
  console.log(a);
}

You need to tell your app to use the variable cors.

Hope this clarifies

1 Like

Thanks a lot. yes, I had forgotten to write app.use(cors());
After using it, I passed all the four tests. wow.
Now. I am going to continue with the last 3 tests remaining in this challenge.
Thanks again. I realized the importance of using cors now.
:slight_smile:
Hopefully I can pass the remaining tests in this challenge.

I passed all the tests in this challenge finally

I managed to pass all the tests with my code. The example code result in the fcc example is rather complicated. But thanks for sharing it, I will save it for my future reference and learning.