MongoError: socket hang up

不想你离开。 提交于 2019-12-12 09:14:48

问题


I am trying to connect to the mongodb database on mongolabs(mlabs). I connect successfully when I run the code on my local computer and server.But When I run on my aws server I get this error database error { [MongoError: socket hang up] name: 'MongoError', message: 'socket hang up' }

Code trial.js:

var express = require('express');
var app = express();
var mongoose = require('mongoose');


var mongojs = require('mongojs');
var db = mongojs('mongodb://user:pass@ds01312192.mlab.com:133492/database', ['mohd'], { ssl : true });

db.on('error', function (err) {
    console.log('database error', err);
});

db.on('connect', function () {
    console.log('database connected');
});

db.mohd.find({}, function (err, docs) { 

    if(err){
        console.log("error");
    }else{
        console.log(docs+"found");
    }

 });

app.set('view engine','ejs');



app.get('/',function(req,res){
  console.log("hi");
});



app.listen(9000,function(){
  console.log("server strated");
});

// catch 404 and forward to error handler

module.exports = app;

Got connection error on Amazon Web Service server but successful on local computer.


回答1:


Ok so I solved the issue it was due to ssl connect method just removed it and was solved. Use Instead:

var db = mongojs('mongodb://user:pass@ds01312192.mlab.com:133492/database', ['mohd']);


来源:https://stackoverflow.com/questions/37156132/mongoerror-socket-hang-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!