试图推桌时的Sequelize,adminer,错误

时间:2018-03-19 15:22:11

标签: mysql node.js database docker sequelize.js

我正在开发一个项目,我正在使用node.js,sequelize,docker以及暂时的混音。但是当我尝试将我的用户表推送到本地数据库时,它给了我这个错误:

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 

我无法在我的代码中找到导致此问题的错误。以下是生成此错误的代码:如果有人可以解释错误或指出我正确的方向,使这项工作非常友好。

型号:

// 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;

控制器:

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!' });
        });
    }
}

多克尔-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"

1 个答案:

答案 0 :(得分:0)

问题不在你的代码中。您收到的ECONNREFUSED显示“拒绝连接”。连接到数据库时出现问题。尝试直接连接到您的数据库,看看它是否正常工作。 THIS URL中的解决方案似乎很有用。