简体   繁体   English

Mongoose 未创建架构或连接到数据库

[英]Mongoose not creating schema, or connecting to database

I have been receiving an error whenever I "post" something using express and node.每当我使用 express 和 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:|我没有意识到你必须安装 mongodb 才能工作:|

so yeah, probably do that所以是的,可能会这样做

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ 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.根据我的经验,几乎每次都是因为在一个项目中安装和使用了两个mongoose模块。 You need to make sure there is only one mongoose in your node_modules .您需要确保node_modules mongoose

Type npm ls mongoose and find all the mongoose packages which are not deduped . npm ls mongoose并找到所有未删除mongoose包。

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.如您所见,我有一个package-one依赖项,它有自己的mongoose模块的副本(未删除)。

There are various ways to solve it.有多种方法可以解决它。

  • Try changing (increasing / decreasing) your mongoose version.尝试更改(增加/减少)您的mongoose版本。
  • If you can, change the required mongoose version of your dependencies (package-one and package-two).如果可以,请更改所需的mongoose版本的依赖项(包一和包二)。

A good start would be to understand how Semantic Versioning works in npm .一个好的开始是了解语义版本控制在 npm 中的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM