#mongodb #express #apple-m1
#mongodb #экспресс #яблоко-м1
Вопрос:
Я установил MongoDB с
brew install mongodb-community@5.0
Я начал MongoDB с
brew services start mongodb-community@5.0
дальше brew services list
я получаю:
Name Status User File emacs stopped mongodb-community started laurenz ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist unbound stopped
На моем экспресс-сервере я использовал этот код для подключения к MongoDB:
connectToServer: function (callback) { mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true }); const db = mongoose.connection db.on('error', (error) =gt; { console.error(error) callback(error) })
}
При открытии MongoDB Compass я могу подключиться к MongoDB, а также увидеть свою таблицу. Тот же код отлично работает на моем Intel iMac.
Это ошибка, которую я получаю при запуске моего server.js:
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at NativeConnection.Connection.openUri (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/connection.js:797:32) at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:332:10 at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 at new Promise (lt;anonymousgt;) at promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:1153:10) at Mongoose.connect (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:331:20) at Object.connectToServer (/Users/laurenz/uni_workspace/omm-project-ws2122/be/db/conn.js:16:14) at Server.lt;anonymousgt; (/Users/laurenz/uni_workspace/omm-project-ws2122/be/server.js:16:7) at Object.onceWrapper (node:events:509:28) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' =gt; [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } } MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at NativeConnection.Connection.openUri (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/connection.js:797:32) at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:332:10 at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 at new Promise (lt;anonymousgt;) at promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:1153:10) at Mongoose.connect (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:331:20) at Object.connectToServer (/Users/laurenz/uni_workspace/omm-project-ws2122/be/db/conn.js:16:14) at Server.lt;anonymousgt; (/Users/laurenz/uni_workspace/omm-project-ws2122/be/server.js:16:7) at Object.onceWrapper (node:events:509:28) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' =gt; [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } } node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^ MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at NativeConnection.Connection.openUri (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/connection.js:797:32) at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:332:10 at /Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 at new Promise (lt;anonymousgt;) at promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:1153:10) at Mongoose.connect (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/lib/index.js:331:20) at Object.connectToServer (/Users/laurenz/uni_workspace/omm-project-ws2122/be/db/conn.js:16:14) at Server.lt;anonymousgt; (/Users/laurenz/uni_workspace/omm-project-ws2122/be/server.js:16:7) at Object.onceWrapper (node:events:509:28) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' =gt; ServerDescription { _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 }, address: 'localhost:27017', type: 'Unknown', hosts: [], passives: [], arbiters: [], tags: {}, minWireVersion: 0, maxWireVersion: 0, roundTripTime: -1, lastUpdateTime: 47955814, lastWriteDate: 0, error: MongoNetworkError: connect ECONNREFUSED ::1:27017 at connectionFailureError (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:293:20) at Socket.lt;anonymousgt; (/Users/laurenz/uni_workspace/omm-project-ws2122/be/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:267:22) at Object.onceWrapper (node:events:510:26) at Socket.emit (node:events:390:28) at emitErrorNT (node:internal/streams/destroy:164:8) at emitErrorCloseNT (node:internal/streams/destroy:129:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) } }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } } Node.js v17.0.1 [nodemon] app crashed - waiting for file changes before starting...
У кого-нибудь есть подобная проблема или кто знает, как это исправить?
Ответ №1:
Так что я понял, в чем была проблема.
Я заменил свой вызов на соединение:
MongoClient.connect('mongodb://127.0.0.1:27017', (err, client) =gt; { if (err) return console.error(err) console.log('Connected to Database') console.log(client); })
Похоже, это сработает. Я не знаю, почему это не сработало с localhost, но теперь это работает.
Комментарии:
1. Спасибо! У меня заканчивалось терпение, Компас и монго могли нормально подключиться к моей базе данных, но мангуст просто отказался бы от ECONNREFUSED. Переход на IP-адрес исправил это мгновенно