我正在尝试通过 post 方法将数据保存在 mongodb 中,并将 mongodb atlas 与 node.js 一起使用,但它一直给我一个错误,我提供了正确的连接字符串和正确的密码。请帮助解决这个问题。谢谢。
这是代码:
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);
});
Run Code Online (Sandbox Code Playgroud)
这是错误:
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:\react-native\reactNativeBackend\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (D:\react-native\reactNativeBackend\node_modules\mongoose\lib\connection.js:808:32)
at Mongoose.connect (D:\react-native\reactNativeBackend\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (D:\react-native\reactNativeBackend\db\db.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:\react-native\reactNativeBackend\index.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)]: {}
}
Run Code Online (Sandbox Code Playgroud)
Aka*_*ari 84
您可以通过进入您的网络访问面板来解决这个问题
然后点击添加IP地址
& 然后填写 0.0.0.0/0 然后点击确认
不要在生产中这样做,但在开发中,它会起作用
小智 21
我有同样的问题,但我成功解决了它。在您的 MongoDB 帐户上,检查您选择连接应用程序的 IP。
如果您开始使用初始 IP 并尝试使用另一个 IP 地址工作,它将无法工作。
在 MongoDB 中:
NetworkAccess/(验证您注册的 IP 与您实际使用的 IP 相同)。您可以选择: 添加 IP 地址
小智 11
我有这个类似的问题。“转到网络访问”->“IP 白名单”-> 您只能从这些 IP 地址连接到您的集群,因此如果您的 IP 不在该列表中
然后单击“添加 IP 地址”按钮 -> 在其中单击“添加您当前的 IP 地址”按钮。
Jes*_*ral 10
这可能是因为您的机器的 IP 发生了变化,在生产中不应该发生这种情况,但要修复它,您需要进入网络访问面板 -> 添加 IP 地址,然后单击“添加此 IP 地址”,然后将其\xc2\ xb4s 会正常工作,直到它再次发生变化。
\n| 归档时间: |
|
| 查看次数: |
39545 次 |
| 最近记录: |