gpt4 book ai didi

javascript - MongooseServerSelectionError: 连接 ECONNREFUSED::1:27017

转载 作者:行者123 更新时间:2023-12-05 00:34:24 27 4
gpt4 key购买 nike

当我尝试使用 Mongoose 将我的应用程序与我的数据库连接时遇到问题。已经尝试了我在谷歌上找到的以下解决方案:

  • windows下重启MongoDB服务
  • 使用位于 mongodb 的 bin 文件中的 cmd 手动打开数据库

  • 但我无法解决。谁能帮我 ?
    //my connection
    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/notes-db-app',{
    useNewUrlParser: true,
    useUnifiedTopology: true
    })
    .then(db => console.log('DB is connected'))
    .catch(err => console.log(err));
    扔给我,这个错误

    MongooseServerSelectionError: connect ECONNREFUSED ::1:27017at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5at new Promise ()at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)at Object. (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
    reason: TopologyDescription {type: 'Unknown',servers: Map(1) { 'localhost:27017' => [ServerDescription] },stale: false,compatible: true,heartbeatFrequencyMS: 10000,localThresholdMS: 15,logicalSessionTimeoutMinutes: undefined}}


    我尝试像这样将端口放在我的连接代码上
    //my connection
    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost:27017/notes-db-app',{
    useNewUrlParser: true,
    useUnifiedTopology: true
    })
    .then(db => console.log('DB is connected'))
    .catch(err => console.log(err));
    它给我带来了另一个错误

    MongooseServerSelectionError: Invalid message size: 1347703880, max allowed: 67108864at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5at new Promise ()at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)at Object. (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
    reason: TopologyDescription {type: 'Unknown',servers: Map(1) { 'localhost:3000' => [ServerDescription] },stale: false,compatible: true,heartbeatFrequencyMS: 10000,localThresholdMS: 15,logicalSessionTimeoutMinutes: undefined}}

    最佳答案

    const uri = 'mongodb://localhost:27017/test';

    const options = {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    serverSelectionTimeoutMS: 5000,
    autoIndex: false, // Don't build indexes
    maxPoolSize: 10, // Maintain up to 10 socket connections
    serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
    socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
    family: 4 // Use IPv4, skip trying IPv6
    }

    const connectWithDB = () => {
    mongoose.connect(uri, options, (err, db) => {
    if (err) console.error(err);
    else console.log("database connection")
    })
    }

    connectWithDB()

    关于javascript - MongooseServerSelectionError: 连接 ECONNREFUSED::1:27017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69840504/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com