【问题标题】:How to check if mongoose is connected?如何检查猫鼬是否已连接?
【发布时间】:2020-09-27 00:25:25
【问题描述】:

在我正在处理的项目中,我像这样连接到数据库并输出 console.log 部分

const express = require('express');
const app = express();
const socketio = require('socket.io');
const mongoose = require('mongoose');

const expressServer = app.listen(3001);
const io = socketio(expressServer);

const Game = require('./Models/Game')
mongoose.connect('mongodb://localhost:27017/test', 
                        {useNewUrlParser: true, useUnifiedTopology: true},
                        ()=>{console.log('successfully connected to database')});

后来,我用猫鼬创建了一个模式模型,创建了这个模型的一个实例,当我尝试在第 13 行保存它时,代码在到达这一点时停止执行。

1    io.on('connect', (socket)=>{
2        socket.on('create-game',async (nickName)=>{
3            try{
4                const quotableData = await QuotableAPI();
5                let game = new Game();
6                game.words = quotableData;
7                let player = {
8                    socketID : socket.id,
9                    isPartyLeader : true,
10                    nickName
11                }
12                game.players.push(player);
13                game = await game.save();
14                .......

所以我想知道是否可能是我没有连接到数据库。作为参考,我试过了

console.log(mongoose.connection.readyState) 

它输出了 0,我在其他帖子中看到这意味着你没有连接。如果是这种情况,如何解决这个问题。

【问题讨论】:

  • 您的代码 '()=>{console.log('successfully connected to database')' 是否真的打印了消息?
  • 是的,它实际上在我的控制台中打印了消息。
  • Tocheie,当我尝试这个时,我得到一个 MongooseError [MongooseServerSelectionError]: connect ECONNREFUSED 127.0.0.1:27017
  • 更新:在本地安装 MongoDB 解决了它

标签: mongodb mongoose socket.io


【解决方案1】:

您可以通过打印连接数据库中的所有集合来验证这一点。

const express = require('express');
const app = express();
const socketio = require('socket.io');
const mongoose = require('mongoose');

const expressServer = app.listen(3001);
const io = socketio(expressServer);
mongoose.connect('mongodb://localhost:27017/test');

mongoose.connection.on('open', function (ref) {
    console.log('Connected to mongo server.');

    mongoose.connection.db.listCollections().toArray(function (err, names) {
        console.log(names);
    });
})

【讨论】:

  • 当我尝试这个时,我得到UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
  • 这适用于我使用我的 Atlas 连接。 mongodb+srv://${mongoClient.username}:${mongoClient.password}@${mongoClient.url}/${mongoClient.database}?retryWrites=true&w=majority
【解决方案2】:

您可以使用mongoose.connection.connect查看。

const mongoClient = require('./config/default')
const mongoose = require('mongoose')

// MongoDB

mongoose.connect(
  `mongodb+srv://${mongoClient.username}:${mongoClient.password}@${mongoClient.url}/${mongoClient.database}?retryWrites=true&w=majority`
)

// Test connection
mongoose.connection.once('open', function () {
  console.log('MongoDB database connection established successfully')
})

您的本地 mongodb 服务器是否正在运行?

在终端输入mongod 启动它。

你也可以使用Mongo Compass来查看。

Here is a good article on it.

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多