Error reading number of items in MongoDB

Tell us what’s happening:
I get a weird error saying

MongooseError: Operation shorturls.findOne() buffering timed out after 10000ms
at Timeout. (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)

whenever I read use the function find().then(), I tried to use count instead of find, using async and await, uninstalling and reinstalling mongoose. I made sure that the database is connected too but still get the error. What would you recommend for me to do?

Code

'use strict';
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const dns = require('dns');
const mongoose = require('mongoose');
var {MongoClient} = require('mongodb');
const bodyParser = require('body-parser');
const app = express();
// Basic Configuration
const port = process.env.PORT || 3000;

const uri = process.env['MONGO_KEY'];
const client = new MongoClient(
                uri, { 
                    useUnifiedTopology: true 
                });

client.connect()
    .then(
        () => console.log('success connecting to database'))
    .catch(
        (err) => console.log(err + '\n' + "fialed to connect to database"));

const regexURL = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i;

const shortUrlScheme = new mongoose.Schema({
    original_url: String,
    short_url: Number
});

const ShortUrl = mongoose.model('ShortUrl', shortUrlScheme);

app.use(cors());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/public', express.static(`${process.cwd()}/public`));

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

const isConnected = () => {
    // used for debugging
    return !!client && 
            !!client.topology && 
            client.topology.isConnected();
}

const findOneByURL = (url, done) => {
    let flag = true;
    console.log('starting search');
    ShortUrl.findOne({original_url: url}, (err, data) => {
        console.log(err);
        if(err)
            return done(err, null);
        flag = false;
        done(null, data);
    });
    console.log('finished search');
    if(flag) done(null, null);
};

const findOneByShort = (url, done) => {
    let flag = true;
    ShortUrl.findOne({short_url: url}, (err, data) => {
        if(err)
            return done(err, null);
        flag = false;
        done(null, data);
    });
    if(flag) done(null, null);
};

const createAndSave = (url, done) => {
    ShortUrl.find().then((err, data) => {
        if(err)
            return done(err, null);
        console.log(data);
        let short = new ShortUrl({
                original_url: url, 
                short_url: data
            });
        short.save((err, data) => {
            if(err)
                return done(err, null);
            let item = {
                original_url: data.original_url,
                short_url: data.short_url
            };
            done(null, item);
        });
    }).catch(
        err => console.log('failed in create and save'));
};

const validateUrl = (value) => {
  return regexURL.test(value);
}

const testUrl = (url, done) => {
    if(validateUrl(url)) {
        dns.lookup(url.replace(/^https?:\/\//i, ''), 
                    (err, address, family) => {
                        if(err)
                            return done(err, null);
                        done(null, address);
                    });
    }
    else {
        done({error: 'invalid url'}, url);
    }
};

app.post('/api/shorturl/new', (req, res) => {
    testUrl(req.body.url, (err, address) => {
        if(err)
            return res.json(err);
        console.log('test succeeded');
        findOneByURL(req.body.url, (err, data) => {
            if(err)
                return res.json(err);
            if(data) {
                console.log('URL Already exists')
                let item = {
                    original_url: data.original_url,
                    short_url: data.short_url
                };
                res.json(item);
            }
            else {
                console.log('to create new item')
                createAndSave(req.body.url, (err, data) => {
                    if(err)
                        return res.json(err);
                    console.log('here');
                    res.json(data);
                    console.log('finished creating');
                });
            }
        });
    });
});

app.get('/api/shorturl/:shortUrl', (req, res) => {
    findOneByShort(req.params.shortUrl, (err, data) => {
        if(err)
            res.json(err);
        if(data) {
            console.log('redirecting');
            res.redirect(data.original_url);
        }
        else {
            res.json({
                error: 'invalid short URL'
            });
        }
    });
});

app.listen(port, function() {
  console.log(`Listening on port ${port}`);
});

Your project link(s)

solution: https://boilerplate-project-urlshortener.ahmedkhfagy.repl.co
githubLink: GitHub - aKhfagy/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36.

Challenge: URL Shortener Microservice

Challenge link: https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice

I traced the problem as much as I can and found out that the the error is logged to the console after he starts executing the createAndSave function

I changed the DNS server from AWS to google cloud and it worked

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.