MongoDB Model.find() does not work inside if conditional branch?

I’m still stuck in API & Microservices test no.3 : URL shorthener. I’ve installed all necessary packages, called them, etc, works fine. I then created my model :

var Schema = mongoose.Schema;
var urlSchema = new Schema({
    url:  {type:String, required:true}

var anUrl = mongoose.model('anUrl', urlSchema);

then handle the post request like so :'/api/shorturl/new', function(req, res){
  const {myUrl} = req.body;
  if(!validURL(myUrl)){//i have defined function validURL() 
    res.json({error: 'invalid URL'});
    dns.lookup(myUrl, function (err, address, family) {
        res.json({error: 'invalid Hostname'});
 //******code between these seems never get called********
       /*I want to check wether or not a particular url have already been 
recorded in the database. If it has, retrieve the short url. If it has not, 
save it, then retreive the short url. However, the code below seems 
unresponsive since the console.log part doesn't display anything at 
glitch console*/
        anUrl.find({url: myUrl}, function(error, urlFind){
          console.log('find : ' + urlFind);//nothing displayed at console
          if (error) return console.error(error);
          console.log('find : ' + urlFind);//nothing displayed at console
/*I have also tried send the urlFind to some other function outside this route 
handler. No response at all*/
 //the code above seems does not called at all..
 //******code between these seems never get called********
        res.send('done : ' + myUrl);

The flow of the above snippet is as follow:

  • validate the url, if it is not valid then res.json({error: 'invalid URL'}) :white_check_mark:
  • If it is a valid url, then check if it is already registered at DNS server. If it has not, then respond with res.json({error: 'invalid Hostname'}) :white_check_mark:
  • If it has not yet been registered at DNS server, then i’m going to check wether this particular submitted url address have already been a recorded document by means of Model.find() function. However, calling this function inside if branch seems does not do anything at all. Please have a look at the snippet above :x:

This is my code so far (edit : link removed)

can you show your anUrl collection structure.

var Schema = mongoose.Schema; 
var urlSchema = new Schema({ url: {type:String, required:true} }); 
var anUrl = mongoose.model('anUrl', urlSchema);

I mean your mongoose database anUrl collections structure .how you are storing value in database.

If what you mean by storing is the save() method, like this below :

var aNewUrl= new anUrl({url: ""});, data) {
    if (err) return console.error(err);
    done(null, data)

then the answer is : i have not implement that yet. First i want to check if the submitted url has already been saved, because i dont want to save the same url twice.

However, if you ask about the collection structure, the answer is above your post.

The problem might be in the validURL function. dns.lookup URLs need to start with “www”, if it starts with “http(s)://“ dns.lookup won’t work. Not knowing what is in that function and the URL you are testing, you may need to write code that ensures the URL is in the above mentioned format.

1 Like

you need some changes in your project :

  1. update mongoose version to 5.8.11.when you update mongoose version, in the console you will see some warnings, to remove these warnings remove {useMongoClient:true} form mongoose.connect() function and add {useNewUrlParser: true,useUnifiedTopology:true} to your connection i.e.
mongoose.connect(process.env.MONGO_URI,  {useNewUrlParser: true, useUnifiedTopology:true}); 

it will remove all warnings.
now start your MONGO_URI with +srv as follows :


(note: store your MONGO_URI inside .env file so nobody can see your MONGO_URI).

after changing above code the result in console when submit url is:

find : 
find : 

anURL.find() returns value of urlFind as empty ,because you have not save any data to your collection yet.

1 Like

Right, thank you again.