Postman socket挂断无法连接到Mongodb [英] Postman socket hang up could not connect to Mongodb

查看:58
本文介绍了Postman socket挂断无法连接到Mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我已经连接了MongoDB Atlas,需要保存一个对象,使用POSTMAN.

但是我收到这个消息 socket 挂断了.我尝试了使用代理选项所能做的一切.

请注意,我的新数据库中没有保存任何内容.除了用户名和密码之外,我还在按原样保留 dbname.以为它会创建一个名为dbname"的数据库.连接时本身.不知道在这里做什么.

DB_CONNECT=mongodb+srv://username:@cluster0.uxjdg.mongodb.net/?retryWrites=true&w=majority

详情如下

Index.js

const express=require('express');const app =express();const mongoose=require('mongoose');const dotenv=require('dotenv');const bodyParser=require('body-parser');dotenv.config();//连接数据库mongoose.connect('process.env.DB_CONNECT',//{ useUnifiedTopology: true },{ useNewUrlParser: true },()=>console.log('连接到数据库'));//中间件app.use(express.json());//导入路由const authRoute=require('./routes/auth');//路由中间件app.use('/api/user', authRoute);app.listen(3000, ()=>console.log('服务器启动并运行'));

路由器

const router=require('express').Router();const User=require('../model/User');router.post('/register', async(req, res)=>{const 用户 = 新用户({名称:req.body.name,电子邮件:req.body.email,密码:req.body.password});尝试{const savedUser=await user.save();res.send(savedUser);}抓住(错误){res.status(400).send(err);}});module.exports=路由器;

架构

<预><代码>const mongoose=require('mongoose');const userSchema =new mongoose.Schema({名称:{类型:字符串,要求:真,分钟:6,},电子邮件:{类型:字符串,要求:真,最大:255,分钟:6},密码:{类型:字符串,要求:真,最大:1024,分钟:6},日期:{类型:日期,默认:Date.now}});module.exports=mongoose.model('User', userSchema);

解决方案

我遇到了和你完全一样的问题,所以我想我会分享我为解决这个问题所采取的步骤:

  • 返回到您的 MongoDB 项目文件夹
  • 删除当前集群并创建新集群
  • 您显然必须重新配置您的数据库访问权限.
  • 我不确定这是否有必要,但我暂时将网络访问权限设置为允许从任何地方进行所有访问"
  • 重新连接到您的集群,即更新您的 DB_CONNECT URL,除非您使用相同的 PW.
  • 也重新启动您的节点.

喂!我希望这有帮助.快乐编码!

i am new in programmaing. I have connected with MongoDB Atlas, and need to save one object,using POSTMAN.

But i get this message socket hang up. I tried what ever was possible using proxy options.

Please note that i do not have anything saved in my new database. apart from username and password, i am keeling dbname as is. thougt it would create a database named "dbname" itself upon connection. not sure what to do here as well.

DB_CONNECT=mongodb+srv://username:@cluster0.uxjdg.mongodb.net/?retryWrites=true&w=majority

Here are the details

Index.js

const express=require('express');
const app =express();
const mongoose=require('mongoose');
const dotenv=require('dotenv');
const bodyParser=require('body-parser');

dotenv.config();

//Connect to DB
mongoose.connect('process.env.DB_CONNECT',
//{ useUnifiedTopology: true },
{ useNewUrlParser: true },

()=>console.log('connect to db'));

//Middleware
app.use(express.json());

//Import Routes

const authRoute=require('./routes/auth');

//Route Middlewares

app.use('/api/user', authRoute);

app.listen(3000, ()=>console.log('Server up and Running'));

Router

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

const User=require('../model/User');

router.post('/register', async(req, res)=>{

    const user =new User({
        name:req.body.name,
        email:req.body.email,
        password:req.body.password
    });

    try{
     const savedUser=await user.save();
     res.send(savedUser);

    }catch(err){
        res.status(400).send(err);
    }
});



module.exports=router;

Schema


const mongoose=require('mongoose');

const userSchema =new mongoose.Schema({
  name:{

    type:String,
    required:true,
    min:6,
  },
  email:{

    type:String,
    required:true,
    max:255,
    min:6
  },

  password:{

    type:String,
    required:true,
    max:1024,
    min:6
  },

  date:{

    type:Date,
    default:Date.now
  }

});

module.exports=mongoose.model('User', userSchema);

解决方案

I had the same exact problem as yours so I thought I'd share the steps that I took to solve this:

  • Go back to your MongoDB project folder
  • Delete the current cluster and create a new cluster
  • You'll obviously have to reconfigure your database access.
  • and I am not sure if this is necessary but I temporarily set the network access to "ALLOW ALL ACCESS FROM ANYWHERE"
  • Reconnect to your cluster i.e. update your DB_CONNECT URL unless your using the same PW.
  • Restart your node as well.

Welp! I hope this helps. Happy coding!

这篇关于Postman socket挂断无法连接到Mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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