Please help. Exercise tracker doesn't save to MongoDB

I made a code on glitch, but somewhy it doesn’t save to the database. Can anyone point on my mistake?

const express = require('express')
const app = express()
const bodyParser = require('body-parser')

const cors = require('cors')

//Setting MongoDB
const mongoose = require('mongoose')
mongoose.connect(process.env.MONGO_URI)




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')
});

//Defining Schema & Model
let Schema = mongoose.Schema;
let userSchema = new Schema({
  user: String
})
let userModel = mongoose.model('Users', userSchema);


//THE POST PROCESS
app.post('/api/exercise/new-user', (req,res)=>{
  let userName = req.body.username;
  
  let userNew = new userModel({user: userName})
  console.log(userNew)
  
  userNew.save()
    .then(res=>{
    console.log('success')
    res.json(res)
  })
    .catch(err=>{
    console.log(err)
    res.json({error: err})
  })
  
})









// 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)
})

Glitch: https://glitch.com/~uatyron-exercise-tracker

2 Likes

When I open your project and remix it for editing it shows me following error in console:

(node:3318) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client

so I use useMongoClient option in connect function i.e. :

mongoose.connect(process.env.MONGO_URI,{useMongoClient:true});

and now it is showing following error:

Error: Invalid mongodb uri "". Must begin with "mongodb://"

since I don’t know what is your mongodb uri and what are you storing inside process.env.MONGO_URI, so check your mongodb uri and put correct mongodb uri in process.env.MONGO_URI inside .env file.

1 Like

Thank you for pointing out this. I checked the package json and found that mondoDb & mongoose were old versions. Updated them to the newer one and now I have a record in a DB.

So the issue was the dependencies in Package.json.

Since they were default - I didn’t check them out, but I should. Thanks for pointing out and helping to resolve that issue.

1 Like

I wasted 3 hours, trying to figure out what was wrong, I just used the glitch link on the project page on fcc, assuming the versions are correct, But as it turns out, they are not!
@fcc please fix the boilerplate!!!

1 Like

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

Hi did you update all packages? what npm command did you use?

on the top of the add-package button of glitch text editor you can update packages, make sure to update mongodb, mongoose, node and body-parser