首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用猫鼬连接到mongodb地图集

无法用猫鼬连接到mongodb地图集
EN

Stack Overflow用户
提问于 2020-02-23 12:01:57
回答 8查看 12.5K关注 0票数 3

我需要帮助!我尝试用连接字符串连接到mongodb地图集,但我无法。我不知道该怎么做。我有一个简单的应用程序。我得到一个连接错误(一个mongooseServerSelectionError)和一个未处理的承诺拒绝警告。

以下是错误:

代码语言:javascript
复制
 connection error:  MongooseError [MongooseServerSelectionError]: connect ECONNREFUSED 127.0.0.1:27017
   at new MongooseServerSelectionError (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/error/serverSelection.js:22:11)
   at NativeConnection.Connection.openUri (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/connection.js:808:32)
   at Mongoose.connect (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/index.js:333:15)
   at Object.<anonymous> (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/server/db/index.js:4:10)
   at Module._compile (internal/modules/cjs/loader.js:1157:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
   at Module.load (internal/modules/cjs/loader.js:1001:32)
   at Function.Module._load (internal/modules/cjs/loader.js:900:14)
   at Module.require (internal/modules/cjs/loader.js:1043:19)
   at require (internal/modules/cjs/helpers.js:77:18)
   at Object.<anonymous> (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/server/bin.js:33:10)
   at Module._compile (internal/modules/cjs/loader.js:1157:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
   at Module.load (internal/modules/cjs/loader.js:1001:32)
   at Function.Module._load (internal/modules/cjs/loader.js:900:14)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
 message: 'connect ECONNREFUSED 127.0.0.1:27017',
 name: 'MongooseServerSelectionError',
 reason: TopologyDescription {
   type: 'Single',
   setName: null,
   maxSetVersion: null,
   maxElectionId: null,
   servers: Map { 'localhost:27017' => [ServerDescription] },
   stale: false,
   compatible: true,
   compatibilityError: null,
   logicalSessionTimeoutMinutes: null,
   heartbeatFrequencyMS: 10000,
   localThresholdMS: 15,
   commonWireVersion: null
 },
 [Symbol(mongoErrorContextSymbol)]: {}
}
(node:9182) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
   at new MongooseServerSelectionError (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/error/serverSelection.js:22:11)
   at NativeConnection.Connection.openUri (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/connection.js:808:32)
   at Mongoose.connect (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/node_modules/mongoose/lib/index.js:333:15)
   at Object.<anonymous> (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/server/db/index.js:4:10)
   at Module._compile (internal/modules/cjs/loader.js:1157:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
   at Module.load (internal/modules/cjs/loader.js:1001:32)
   at Function.Module._load (internal/modules/cjs/loader.js:900:14)
   at Module.require (internal/modules/cjs/loader.js:1043:19)
   at require (internal/modules/cjs/helpers.js:77:18)
   at Object.<anonymous> (/Users/jagnoorg/Documents/personal-projects/monkey-den/project2/server/bin.js:33:10)
   at Module._compile (internal/modules/cjs/loader.js:1157:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
   at Module.load (internal/modules/cjs/loader.js:1001:32)
   at Function.Module._load (internal/modules/cjs/loader.js:900:14)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
(node:9182) 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:9182) [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.

这是我的代码:

我给出了用于连接uri的说明,通过mongoose设置连接,导出db并在app.js中打开连接。

内部db/index.js

代码语言:javascript
复制
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
// const uri = require('../../keys.js').mongodb.dbURI;
mongoose.connect("mongodb://localhost/test", { useNewUrlParser: true, useUnifiedTopology: true});
// mongoose.connect(process.env.MONGODB_URL || uri, { useNewUrlParser: true, useUnifiedTopology: true});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));

module.exports = db;

内部app.js

代码语言:javascript
复制
console.log('NODE_ENV :::', process.env.NODE_ENV);
if (process.env.NODE_ENV === 'PRODUCTION') {
    var app = require('./prod.app.js'); 
} else {
    var app = require('./dev.app.js');
}
var express = require('express');
var path = require('path');
var routes = require('./db/routes/index.js');
var http = require('http');
var favicon = require('serve-favicon');
var PORT = 3000;

/* 
 * shared code in production and development 
*/

// favicon - 3 ways - middleware, static, html 
app.use(favicon(path.join(__dirname, '../public/images/favicon.ico')));
// app.use('/favicon.ico', express.static(path.join(__dirname, '../public/images/favicon.ico')));
// app.use('images/favicon.ico', express.static(path.join(__dirname, '../public/images/favicon.ico')));

app.use(express.json());
app.use(express.urlencoded({ extended: true}));

// static files
app.use(express.static(path.join(__dirname, "../client")));
app.use(express.static(path.join(__dirname, "../public")));

// db - mongo
var db = require('./db/index.js');
db.once("open", function (err) {
    // console.log('db::: ', db);
    if (err) {
        return console.error(err);
    } else {
        console.log("Successfully connected to mongodb");
    }
})

// routes
app.use(routes); 

// serve index.html if can't find the route
app.use('*', function (req, res, next) {
    console.log("...serving index.html file from the public folder")
    res.sendFile(path.join(__dirname, '../public/index.html'));
});

// create server 
var server = http.createServer(app);
server.listen(PORT);
console.log(`server listening on post ${PORT}`);

谢谢你帮我调查这个

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2020-03-01 09:26:51

请检查您的IP是否由mongodb地图启用。您应该从mongodb地图集访问您的计算机IP地址。转到网络访问(左侧栏),输入IP或输入0.0.0.0访问所有IP。

票数 4
EN

Stack Overflow用户

发布于 2020-03-01 11:12:41

您的app.js app.use(cors())中有这一行吗?

尝试删除这一行并删除所有涉及cors的内容。

如果这不起作用,请检查MongoDB Atlas中网络访问内部的IP白名单,并添加当前IP或编写此0.0.0.0/0以允许所有IP。

票数 7
EN

Stack Overflow用户

发布于 2020-03-04 13:59:00

在连接ip地址中的mongoDB中,将当前ip地址设置为0.0.0.0/0。它将允许连接所有ip地址。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60361994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档