我对这个后端东西完全陌生,这是我的代码,当我试图将猫鼬连接到数据库时,将显示此错误。
我已经进行了彻底的搜索,找到了这个示例[MongoError: connection 0 to localhost:27017 timed out,但是我不知道如何在我的项目中使用它。
{ MongoNetworkError: connection 0 to localhost:27017 timed out
at Socket.<anonymous>
(C:\Users\ABCD\Pictures\Freelancer\node_modules\mongodb-
core\lib\connection\connection.js:259:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:422:8)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
THIS IS MY CODE
const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
//HANDLEBAR MIDDLEWARE
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//Connect to mongoose
mongoose.connect('mongodb://localhost/freelancer-dev',{
useNewUrlParser: true
})
.then(()=>console.log('mongodb connected'))
.catch(err=> console.log(err));
const port = process.env.PORT ||5000;
app.listen(port,()=>{
console.log(`listening at port${port}`);
});