首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sequelize,adminer,尝试推送表时出错

Sequelize,adminer,尝试推送表时出错
EN

Stack Overflow用户
提问于 2018-03-19 23:22:11
回答 1查看 51关注 0票数 0

我正在开发一个项目,其中我使用了node.js,sequelize,docker和adminer。但是,当我试图将我的用户表推送到本地数据库时,它给出了这个错误:

代码语言:javascript
复制
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 

我在我的代码中找不到导致这个问题的bug。这是产生这个错误的代码:如果有人能解释这个错误,或者给我指出正确的方向,让这个工作,那就太好了。

型号:

代码语言:javascript
复制
// 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;

控制器:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

发布于 2018-03-19 23:49:30

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49366499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档