mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Error: read ECONNRESET at TCP.onStreamRead

Open joaogoya opened this issue 7 years ago • 5 comments

I having this error { Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js: 111: 27) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError'], [Symbol(mongoErrorContextSymbol)]: {} }

Package.json:

{
  "name": "revisao01-mar-2018",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./bin/server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.3",
    "debug": "^3.2.6",
    "express": "^4.16.4",
    "guid": "0.0.12",
    "http": "0.0.0",
    "joigoose": "^4.0.5",
    "mongoose": "^5.5.3",
    "validator": "^10.11.0"
  }
}

index.js

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

const app = express();
const router = express.Router();

mongoose.connect('myDbUrl',  {useNewUrlParser: true })
  .then(() => {
    server.start();
  })
  .catch((err) => {
    console.log(err.stack);
    process.exit(1);
  });

//models
const Product = require('./models/product');
const Customer = require('./models/customer');
const Order = require('./models/order');

// roteamento
const rotaIndex = require('./routes/index');
const rotaProducts = require('./routes/rotaProducts');
const rotaCustomer = require('./routes/rotaCustomer');
const rotaOrder = require('./routes/rotaOrder');

app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extend:false }));
app.use(bodyParser.urlencoded({ extended: false }));

app.use("/", rotaIndex);
app.use("/products", rotaProducts);
app.use("/customer", rotaCustomer);
app.use("/order", rotaOrder);

module.exports = app; 

server.js

const http = require('http');
const debug = require('debug');
const express = require('express');
const app = require('../src/index');

const port = normalizrePort(process.env.PORT || "3000");
app.set('port', port);

const server = http.createServer(app);

server.listen(port);
server.on("error", onError);
server.on("listening", onListening);


console.log('Aplicação rodando na porta '+ port);

function normalizrePort(val) {
    const port = parseInt(val, 10);
    if (isNaN(port)) {
      return val;
    }
    if (port >= 0) {
      return port;
    }
  }
  
  function onError(error) {
    if (error.syscall !== "listen") {
      throw error;
    }
  
    const bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
  
    switch (error.code) {
      case "EACCES":
        console.error(bind + " requires elevated privileges");
        process.exit(1);
        break;
      case "EADDRINUSE":
        console.error(bind + " is already in use");
        process.exit(1);
        break;
      default:
        throw error;
    }
  }
  
  function onListening() {
    // debug do serv
    const addr = server.address();
    const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
    debug("Listening on " + bind);
  }

joaogoya avatar Apr 24 '19 18:04 joaogoya

Can you show the code that's starting a transaction? You're getting a TransientTransactionError, so the code you've shown thus far cannot cause that error.

vkarpov15 avatar Apr 26 '19 22:04 vkarpov15

Model: https://github.com/joaogoya/Order-System/blob/master/sistema/src/models/product.js

Repository: https://github.com/joaogoya/Order-System/blob/master/sistema/src/repository/product-repository.js

Controller https://github.com/joaogoya/Order-System/blob/master/sistema/src/controllers/product-controller.js

Route: https://github.com/joaogoya/Order-System/blob/master/sistema/src/routes/rotaProducts.js

I researched and discovered that it is a problem related to the way mongoose treats rejected promises.

But despite finding the problem, I could not get my code to work.

Any suggestion? Thank you. Hugs

joaogoya avatar Apr 29 '19 01:04 joaogoya

Hi, @vkarpov15 . I put a catch in .connect ().

mongoose.connect('').catch((e) => console.error(e));

This error appears:

{ Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ], [Symbol(mongoErrorContextSymbol)]: {} }

joaogoya avatar Apr 30 '19 13:04 joaogoya

@joaogoya you said you found the problem, can you describe what the problem was? I still don't understand.

You might want to take a look at what causes ECONNRESET on Node.js. The most likely explanation for this error is that you lost connection to the mongodb server due to the mongodb server crashing or some network issue.

vkarpov15 avatar May 06 '19 16:05 vkarpov15

@vkarpov15

First I identified that the application was having problems with rejected promises. I kept investigating and I saw that the problem is with the connection. Nothing is coming from the data base, but the data base is ok. That's why I opened this post here, i thought it was mongoose.

But I kept working on that and I saw that it is a problem of environment variable. On the computers of some co-workers the application works perfectly. It's something on my pc.

So, I do not think it's mongoose. This post may close.

But many thanks for the help. hug.

joaogoya avatar May 08 '19 13:05 joaogoya