【问题标题】:Sequelize, adminer, error when trying to push tableSequelize,管理员,尝试推送表时出错
【发布时间】:2018-08-28 05:19:13
【问题描述】:

我正在开发一个项目,我正在使用 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!' });
        });
    }
}

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"

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 2016-03-18
      • 2022-10-14
      • 1970-01-01
      • 2016-08-27
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多