URL Shortener Microservice:mpromise (mongoose's default promise library) is deprecated

Tell us what’s happening:
Hi all,
I’m working on URL Shortener Microservice.

In the first step I tried to save something that send to the server via POST method in the database with this code

Your code so far

'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.MONGOLAB_URI);

app.use(cors());

/** this project needs to parse POST bodies **/
// you should mount the body-parser here
var bodyParser = require('body-parser');
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req, res){
  res.sendFile(process.cwd() + '/views/index.html');
});

  
// your first API endpoint... 
app.get("/api/hello", function (req, res) {
  res.json({greeting: 'hello API'});
});
////////////////////////////////////////////////////////////////////////////////////
mongoose.connect(process.env.MONGO_URI);

var Schema = mongoose.Schema;
var url = new Schema({
  name : {type:String},
  shortUrl : {type:Number}
});

var Url = mongoose.model('Url', url);


app.post('/api/shorturl/new',
         function(req,res){
  var newUrl = new Url({name:req.body.url, shortUrl : 1});

  
    newUrl.save(function (err, newUrl) {
    if (err) return console.error(err);
    console.log(newUrl.name+' saved sucessfully.');
  });
  res.json({name:req.body.url})
});

////////////////////////////////////////////////////////////////////////////////////

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

But I got an error as below in the console and nothing saved in my database:
(node:127) DeprecationWarning: Mongoose: mpromise (mongoose’s default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

I don’t understand the error and how can I debug my code?

Link to the challenge:
https://learn.freecodecamp.org/apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice

I’m not sure that DeprecationWarning has something to do with the fact that nothing is saved. Can you check your Mongoose connection like this?

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true})
  .then(() => console.log('Connected'))
  .catch(error => console.log(error));

I’m pretty sure that the root of your problem is .env as you need dotenv package initialized to use custom environmental variables and I don’t see it in your code.

1 Like

Thank you very much iiiked,
I checked my Mongoose connection and got this error:
Error: Invalid mongodb uri “mongodb+srv://hd_mohseni:my-password-is-here@cluster0-grywt.mongodb.net/test?retryWrites=true&w=majority”. Must begin with “mongodb://”

It seems the uri isn’t acceptable.

But I just copy the uri from this section of mongodb.com:

and I don’t how can I correct my uri?
can you help me please?
thanks

The error massage was about the version of mongodb and mongoose that I’v used.
this link of stackoverflow.com helped me:
https://stackoverflow.com/questions/48980473/invalid-mongodb-uri-must-begin-with-mongodb