Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

node.js - MongooseError [MongooseServerSelectionError]: connection <monitor> to 52.6.250.237:27017 closed

I am trying to save data in mongodb through post method and using mongodb atlas with node.js but it keeps giving me an error, I provided correct connection string and correct password. Please help in regarding this issue. Thanks.

Here is the code:

const mongoose = require('mongoose');

const monngodb_url="mongodb+srv://maarij:asdf360@cluster0-j7wis.mongodb.net/authDB?retryWrites=true&w=majority";

mongoose.connect(monngodb_url, {useNewUrlParser: true,useUnifiedTopology: true}).then(()=>{
    console.log("mongodb is connected");
}).catch((error)=>{
    console.log("mondb not connected");
    console.log(error);
});

Here is the error:

server is running on port 3000
{ name: 'myName', email: 'e@mail.com', password: 'abcdefg' }
mondb not connected
MongooseError [MongooseServerSelectionError]: connection <monitor> to 52.6.250.237:27017 closed
    at new MongooseServerSelectionError (D:
eact-native
eactNativeBackend
ode_modulesmongooseliberrorserverSelection.js:22:11)
    at NativeConnection.Connection.openUri (D:
eact-native
eactNativeBackend
ode_modulesmongooselibconnection.js:808:32)
    at Mongoose.connect (D:
eact-native
eactNativeBackend
ode_modulesmongooselibindex.js:333:15)
    at Object.<anonymous> (D:
eact-native
eactNativeBackenddbdb.js:5:10)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (D:
eact-native
eactNativeBackendindex.js:4:19)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) {
  message: 'connection <monitor> to 52.6.250.237:27017 closed',
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'cluster0-shard-00-01-j7wis.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-02-j7wis.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-00-j7wis.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You Can Solve This Problem By Going into Your Network Access Panel

Network Panel

Then Click Add IP Address

Add Ip Address

& Then Fill 0.0.0.0/0 Then Click Confirm

Don't Do this in Production But in Development, it will Work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...