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!
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/
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.
mongoose
version.mongoose
version of your dependencies (package-one and package-two).A good start would be to understand how Semantic Versioning works in npm .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.