Невозможно соединиться с Mon goose (Mongodb) - PullRequest
0 голосов
/ 23 февраля 2020
const express=require('express');
const mongoose=require('mongoose');
const morgan=require('morgan');
const path=require('path');


const app = express();
const PORT = process.env.PORT || 8080;


mongoose.connect('mongodb://localhost/SUCCESS', {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

mongoose.connection.on('connected', () => {
    console.log('Mongoose is connected');


})

//HTTP request logger
app.use(morgan('tiny'));

//Routes
app.get('/', (req, res) => {
const data = {
    username: 'caa',
    age: 5
};
res.json(data);

});

app.get('/api/name', (req, res) => {
const data = {
    username: 'caa',
    age: 5
};
res.json(data);

});

app.listen(PORT, console.log(`Server is starting at ${PORT}`));

node pages/server.js
Server is starting at 8080
(node:6253) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at new MongooseServerSelectionError (/Users/Abc/Desktop/success/node_modules/mongoose/lib/error/serverSelection.js:22:11)
    at NativeConnection.Connection.openUri (/Users/Abc/Desktop/success/node_modules/mongoose/lib/connection.js:808:32)
    at Mongoose.connect (/Users/Abc/Desktop/success/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/Users/Abc/Desktop/success/pages/server.js:18:10)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
(node:6253) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6253) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Ответы [ 2 ]

0 голосов
/ 02 мая 2020

У меня была такая же проблема. Пожалуйста, обновите ваш код следующим образом.

mongoose.connect("mongodb://127.0.0.1:27017/SUCCESS",{
        useCreateIndex:true,
        useNewUrlParser: true,
        useUnifiedTopology: true}).then(()=> {
console.log('Database Successfully Connected')},error =>{
console.log(error)})

В качестве следующего шага

  1. Go в диспетчере задач
  2. Службы
  3. Пуск 'MongoDB'

Надеюсь, это сработает.

0 голосов
/ 23 февраля 2020

// Похоже, вы не определили порт по умолчанию для man goose. Пожалуйста, обратитесь под кодом

const mongoose = require('mongoose');
const db = "mongodb://localhost:27017/SUCCESS";

mongoose.connect(db, {
        useCreateIndex:true,
        useUnifiedTopology:true,
        useNewUrlParser:true
    }).then( () => {
          console.log("Connected To Mongo Db DataBase");
      }).catch((err) => {
        console.log("DataBase Connection Error " + err);
    })
...