MongoError:拓扑已关闭,请连接 [英] MongoError: Topology is closed, please connect

查看:43
本文介绍了MongoError:拓扑已关闭,请连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名前端开发人员,正在尝试在一个新的 Next 项目上拓宽视野,第一次学习 Node、Mongo 和 GraphQL 的服务器端.Apollo 对我来说是最简单的入门方式,因为我已经在之前的项目中使用了客户端 Apollo.

我一直在关注

I'm a front-end dev trying to expand my horizons on a new Next project, learning Node, Mongo, and the server side of GraphQL for the first time. Apollo strikes me as the easiest way to jump in, as I've already used client-side Apollo on previous projects.

I've been following the official docs, where I learned of apollo-datasource-mongodb (seemingly the best way to plug my Apollo Server straight into a local Mongo database. Unfortunately there don't seem to be any example repos of this package in action for me to cheat off of so I've been left to muddle through.

I have mongo running locally via mongod and I can perform successful find() queries through the mongo shell, so I know the database itself is in good shape and contains nearly 600,000 records (I'm working with a rather large dataset).

I also have access to the Apollo Playground at localhost:4000 so I know the server is starting properly and connecting to the database (with appropriate Schema tips/errors I've since managed to resolve).

Here's the query I'm using in the Playground:

{
  item(id: 22298006) {
    title
  }
}

and here's what I'm getting back in response:

{
  "errors": [
    {
      "message": "Topology is closed, please connect",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "item"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "name": "MongoError",
          "stacktrace": [
            "MongoError: Topology is closed, please connect",

            ...

          ]
        }
      }
    }
  ],
  "data": {
    "item": null
  }
}

I've attached my server file below. I have a suspicion that this might be some kind of timeout error, like it takes to long to comb through all 600k records to find the one with the ID I provided? When I remove useUnifiedTopology: true from the MongoClient definition I get a different error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

but I'm not using async or promises myself. Which has me thinking — should I be? Could I somehow hold up the process while I wait for the return from findOneById() (if that is, indeed, the problem)?

As an aside, I've seen at least one example codebase where MongoClient included within itself a Server declaration (also from the 'mongodb' npm package). Would implementing something like that save me from having to block up a terminal window with mongod every time I want to work on my project?

Thank you so much for your time! If I can get this working I'll definitely do a full writeup on Medium or something to pave the way for others looking to pair MongoClient with ApolloServer for a quick and easy API.

index.js

const { MongoClient } = require('mongodb');
const assert = require('assert');
const { ApolloServer, gql } = require('apollo-server');
const { MongoDataSource } = require('apollo-datasource-mongodb');


const client = new MongoClient('mongodb://localhost:27017/projectdb', { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
  err && console.log(err);
});

client.connect((err) => {
  assert.equal(null, err);
  client.close();
});

const db = client.db();

class Items extends MongoDataSource {
  getItem(id) {
    return this.findOneById(id);
  }
}

const typeDefs = gql`
  type Item {
    id: Int!
    title: String!
  }
  type Query {
    item(id: Int!): Item
  }
`;

const resolvers = {
  Query: {
    item: (_, { id }, { dataSources }) => dataSources.items.getItem(id),
  }
}

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => ({
    items: new Items(db.collection('items')),
  }),
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${ url }`);
});

解决方案

Actually there is one more thing that may be causing this as I also had the same exact error "Topology Closed, please connect".

The thing is if you have a dynamic IP address then in MongoDB atlas you should try allowing all IP addresses.

Add the IP address: 0.0.0.0/0 All my problems were solved after whitelisting this IP address which allows all.

Image of dynamic IP 0.0.0.0/0:

这篇关于MongoError:拓扑已关闭,请连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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