0

I'm working on a projekt where i'm using node.js, sequelize, docker and for the time being adminer. But when i try to push my user-table to the local database it gives me this error:

errno: 'ECONNREFUSED',
server_1       |      code: 'ECONNREFUSED',
server_1       |      syscall: 'connect',
server_1       |      address: '172.18.0.4',
server_1       |      port: 3000,
server_1       |      fatal: true 

I'm unable to find the bug in my code that causes this. Here is the code that generates this error: If someone could explain the error or point me in the right direction to make this work that would be very kind.

Model:

// The User model.
'use strict';

var Sequelize = require('sequelize'),
    bcrypt = require('bcrypt');

var config = require('../config'),
    db = require('../services/database');

// 1: The model schema.
var modelDefinition = {
    username: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false,
        validate: {
          len: [4,15]
        }
    },

    password: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
          len: [8,100]
        }
    },

    email: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
        validate: {
          isEmail: true,
          len: [2,100]
        }
    },

    active: {
      type: Sequelize.BOOLEAN,
      defaultValue: false
    },

    temporarytoken: {
      type: Sequelize.STRING
    },

    resettoken: {
       type: Sequelize.STRING
     },

    role: {
        type: Sequelize.INTEGER,
        defaultValue: config.userRoles.user
    }
};

// 2: The model options.
var modelOptions = {
    instanceMethods: {
        comparePasswords: comparePasswords
    },
    hooks: {
        beforeValidate: hashPassword
      },
    classMethods: {
      associate: function(models) {
        UserModel.belongsToMany(models.OrgModel, { through: 'UserProject'}, {
          onDelete: 'cascade'
        });
      }
    }
};

// 3: Define the User model.
var UserModel = db.define('user', modelDefinition, modelOptions);

// Compares two passwords.
function comparePasswords(password, callback) {
    bcrypt.compare(password, this.password, function(error, isMatch) {
        if(error) {
            return callback(error);
        }

        return callback(null, isMatch);
    });
}

// Hashes the password for a user object.
function hashPassword(user) {
    if(user.changed('password')) {
        return bcrypt.hash(user.password, 10).then(function(password) {
            user.password = password;
        });
    }
}

module.exports = UserModel;

Controller:

var config = require('../config'),
    db = require('../services/database'),
    User = require('../models/user'),
    Organisation = require('../models/organisation'),
    Event = require('../models/event');

// The authentication controller.
var AuthController = {};

// Register a user.
AuthController.signUp = function(req, res) {
    if(!req.body.username || !req.body.password || !req.body.email) {
        res.json({ message: 'Please provide a username and a password.' });
    } else {
      var token = jwt.sign({ username: req.body.username }, config.keys.secret, { expiresIn: '30m' });
        db.sync().then(function() {

            var newUser = {
                username: req.body.username,
                password: req.body.password,
                email: req.body.email,
                temporarytoken: token
            };
            // NEED TO CHECK FOR ERRORS BEFORE THIS LINE OF CODE
            return User.create(newUser).then(function() {

                // CREATE EMAIL OBJECT TO SEND TO USER

                res.status(201).json({ message: 'Account created!' });
            });
        }).catch(function(error) {
          console.log(error);
            res.status(403).json({ message: 'Username already exists!' });
        });
    }
}

Docker-compose.yml:

version: '3'
services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  client:
    build: ./client
    ports:
     - "3001:3000"
    volumes:
     - "./client:/app"
    environment:
      - VIRTUAL_HOST=ticketgo.local

  server:
    build: ./server
    ports:
     - "3000:3000"
    volumes:
     - "./server/src:/app/src"
    links:
     - "database"
    environment:
      - VIRTUAL_HOST=api.ticketgo.local

  database:
    image: mysql
    environment:
      MYSQL_DATABASE: "ticketgo"
      MYSQL_ROOT_PASSWORD: "pass"
    volumes:
     - "./sql:/docker-entrypoint-initdb.d"

  adminer:
    image: "adminer"
    ports:
     - "8080:8080"
    links:
     - "database"
Oscar
  • 221
  • 2
  • 7
  • 18

1 Answers1

0

The issue is not in your code. You are getting ECONNREFUSED which shows "Connection Refused". There is an issue while connecting to your database. Try a direct connection to your database to see if it's working. the solution in THIS URL seems helpful.

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79