URL Shortener project - not saving newUrl to mongoDB

I wrote the below code so far but it seems like “newUrl” is not created or saved in mongoDB.

Could anyone help me on this?

'use strict';

var express = require('express');
var mongo = require('mongodb');
var mongoose = require('mongoose');

var cors = require('cors');

var app = express();

// Basic Configuration 
var port = process.env.PORT || 3000;

/** this project needs a db !! **/ 
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true }, (error, client) => {
	console.log("Successfully connected to MongoDB");

/** this project needs to parse POST bodies **/
// you should mount the body-parser here

const bodyParser = require('body-parser');
app.use('/public', express.static(process.cwd() + '/public'));
app.get('/', function(req, res){
  res.sendFile(process.cwd() + '/views/index.html');
app.use(bodyParser.urlencoded({ extended: false }))
//crete model
const Schema = mongoose.Schema;
const urlSchema = new Schema({
  shortid: { type: Number},
let Url = mongoose.model('Url', urlSchema);

app.post('/api/shorturl/new', (req, res) => {
let originalUrl = req.body.url;
var number = Math.floor(Math.random()*10000).toString();
let newUrl = new Url({
  shortid: parseInt(number),

  newUrl.save((err, data) => {
    if (err)
      throw err

return res.json({"original_url":newUrl.original_url,"short_url":newUrl.shortid});


app.listen(port, function () {
  console.log('Node.js listening ...');

hi Eriko87
i tested your code locally on my computer and it works fine.
are you connected to your database?
can you console.log(originalUrl) ?

Thank you for checking!
Yes, I can see console.log(originalUrl) and console.log(newUrl) -

{ original_url: 'https://www.freecodecamp.org',
shortid: 1668,
_id: 5d361ab16fe6760314535da7 }

. I thought I should be able to see it on MongoDB in below section but I guess not? (I am super new on mongoDB)

if not, where is it saved and I can I check the saved data?

It should be connected to the database.

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true }, (error, client) => {
	console.log("Successfully connected to MongoDB");

I see “Successfully connected to MongoDB” in my console.

databases 0 collections 0
is your MONGO_URI ends with the name of your database?

I have the below in my .env


I updated “mongodb”: “^3.2.7”, “mongoose”: “^5.6.6” and finally be able to connect to the DB.

1 Like