• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

249
Views
Mongoose not creating schema, or connecting to database

I have been receiving an error whenever I "post" something using express and node. i can load the website, but it crashes once i submit my form data

This has been the result:

/Users/anishladdha/url_shortener/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185
          callback(new MongooseError(message));
                   ^

MongooseError: Operation `urls.findOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> 

when i run this code:

const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const https = require('https')
const { json } = require('body-parser');
const mongoose = require('mongoose');
const shortID = require('shortid');
const URL = require("./models/url.js")

const app = express();
app.set('view engine', 'ejs');

var mongoDB = 'mongodb://localhost/url_short';
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true}).then(console.log("connected?"));
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once("open", function() {
  console.log("MongoDB database connection established successfully");
});
app.use(bodyParser.urlencoded({extended: true}));


//app.get('/') works
app.post('/', async function(req, res) {
    let sid = shortID.generate
    await URL.create({
        longUrl: req.body.user_url,
        shortUrl: sid,
    });
    res.render('index', {sid: sid});
});
app.listen(3000, function() {
    console.log('listening on port:3000');
});

any help would be appreciated!

over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

OK, so this was quite possibly the stupidest thing i've ever done in my yers of coding.

I didnt realize you had to install mongodb for it to work :|

so yeah, probably do that

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

over 3 years ago · Santiago Trujillo Report

0

In my experience, almost every time the reason is two mongoose modules installed and used in a project. You need to make sure there is only one mongoose in your node_modules.

Type npm ls mongoose and find all the mongoose packages which are not deduped.

Example:

$ npm ls mongoose
my-project@1.0.0 /home/jon/code/my-project
├─┬ package-one@2.0.0
│ └── mongoose@5.11.18
├─┬ package-two@4.0.0
│ └── mongoose@5.12.2  deduped
└── mongoose@5.12.2 

As you can see I have the package-one dependency which has its own (not deduped) copy of mongoose module.

There are various ways to solve it.

  • Try changing (increasing / decreasing) your mongoose version.
  • If you can, change the required mongoose version of your dependencies (package-one and package-two).

A good start would be to understand how Semantic Versioning works in npm.

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error