MongoError:拓扑已关闭,请连接似乎与 MongoClient.close() 相关,并且客户端在使用时无法工作 [英] MongoError: Topology is closed, please connect seems to be related to MongoClient.close() and the client won't work while using it

查看:41
本文介绍了MongoError:拓扑已关闭,请连接似乎与 MongoClient.close() 相关,并且客户端在使用时无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的这个问题密切相关,但问题和答案是完全没有关系.但是,在该 OP 帖子的评论中,使用 .close() 存在困难......这就是相似之处.

This question here is closely related but the question and the answer are not very related at all. However, in the comments of that OP's post there is the difficulty of working with .close()... That is where the similarities end.

问题来自尝试使用直接 nodejs mongodb 驱动程序 3.9 创建 api.我在控制器中有客户端调用,因为将它放在服务器中并在需要时调用客户端不断地创建连接错误",因此它在控制器中用于完整上下文.

The issue comes from trying to create an api using the direct nodejs mongodb driver 3.9. I have the client call in the controller because putting it in the server and calling to the client when needed constantly creates a "connect error" so here it is in the controller for full context.

每当我在集合客户端调用中使用 .close() 时,它都会运行一次,然后每次其他调用都会导致错误拓扑关闭.所以,现在我把它注释掉了.我想知道是什么导致了这个,我可能做错了什么导致这个错误?

whenever I use .close() in the collection client call it will run once and then every other call after that will cause an error Topology is closed. So, for now I commented it out. I am wondering what is causing this and what might I being doing incorrectly causing this error?

每个驱动程序的文档说明使用它以及目的是什么,但它反复破坏我的 api.

The documentation per the driver states to use this and what the purpose is but it is breaking my api repeatedly.

const express = require('express');
const router = express.Router();

import { MongoClient, MongoCallback, MongoError, MongoNetworkError } from 'node_modules/mongodb';
// const MongoClient = require('node_modules/mongodb').MongoClient;
const dbName = 'HealthCheckup';
const documentName = 'userProfile';
const assert = require('assert');
const url = `mongodb+srv://UserDB:h7srvvvvvvvHFd8@vvvvvvvvvai-cvyrh.azure.mongodb.net/${dbName}?retryWrites=true&w=majority`;
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true});

const findUsers = (client, callback) => {
    // Get the documents collection
    const collection = client.collection(documentName);
    // Insert some documents
    collection.find({}).toArray( (err, result) => {
        // console.log('err **** -------- ', err);
        // console.log('Result -------- ', result);
        callback(err, result);
    });
}

const createUser = (client, callback, req) => {
    // Get the documents collection
    const collection = client.collection(documentName);
    // Insert some documents
    collection.insertOne({
        name: req.body.name,
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        email: req.body.email,
        tokenId: req.body.tokenId,
        userPhoto: req.body.userPhoto
    }, (err, result) => {
        // console.log('err **** -------- ', err);
        // console.log('Result -------- ', result);
        callback(err, result);
    });
}

// localhost:4021/user
router.get('/', (req, res) => {
    client.connect( err => {
        const collection = client.db(dbName);
        if (!err) {
            console.log("Connected successfully to server.");
        } else {
            console.log('Error in DB connection : ', JSON.stringify(err, undefined, 2));
        }
        findUsers(collection, (err, result) => {
            console.log('err 2222222 -------- ', err);
            console.log('Result 2222222 -------- ', result);
            if (!err) { 
                res.send(result); 
            } else {
                console.log('Error retreiving user ', JSON.stringify(err, undefined, 2))
            }
            console.log('BREAKPOINT 00000000000000000');
            // client.close();
        });
    });
});

推荐答案

您正在尝试关闭请求处理程序中的客户端,但您的客户端是全局的.

You are trying to close the client in a request handler, but your client is global.

如果您想要一个全局客户端,请不要在请求处理程序中关闭它.

If you want to have a global client, do not close it in request handlers.

如果要关闭请求处理程序中的客户端,请在同一请求的处理程序中创建客户端.

If you want to close the client in request handlers, create the client in the handler of the same request.

这篇关于MongoError:拓扑已关闭,请连接似乎与 MongoClient.close() 相关,并且客户端在使用时无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆