通过Sequelize(Express / NodeJS)访问MySQL时出错

我正在尝试使用Sequelize。 我虽然想到了一些错误,但清除了其中的一些,但是我无法弄清楚。

项目结构:

Project -Client -Server --models ---index.js ---Sid.js --router ---routes ----signup.js ---index.js app.js 

我的configuration可变:

 "Lexstart": { "dbConfig": { "driver": "mysql", "user": "root", "database": "sid1", "password": "sid1", "host": "127.0.0.1", "port": "3306" } } 

我的模特/ index.js:

 var fs = require("fs"); var path = require("path"); var Sequelize = require('sequelize'); var config = require('config'); // we use node-config to handle environments var dbConfig = config.get('Lexstart.dbConfig'); // initialize database connection var sequelize = new Sequelize( dbConfig.database, dbConfig.username, dbConfig.password,{ host: dbConfig.host, dialect: dbConfig.driver } ); var db = {}; fs .readdirSync(__dirname) .filter(function(file) { return (file.indexOf(".") !== 0) && (file !== "index.js"); }) .forEach(function(file) { var model = sequelize.import(path.join(__dirname, file)); db[model.name] = model; }); Object.keys(db).forEach(function(modelName) { if ("associate" in db[modelName]) { db[modelName].associate(db); } }); db.sequelize = sequelize; db.Sequelize = Sequelize; module.exports = db; 

车型/ Sid.js

 module.exports = function(sequelize, DataTypes) { var Sid = sequelize.define("Sid", { username: DataTypes.STRING, password: DataTypes.STRING }); return Sid; }; 

我的路由器/ index.js

 /** * The Index of Routes */ module.exports = function (app) { // The signup route app.use('/signup', require('./routes/signup')); } 

我的路由器/路由/ signup.js

 var models = require('../../models'); var Sid = models.Sid; // Include Express var express = require('express'); // Initialize the Router var router = express.Router(); var config = require('config'); // we use node-config to handle environments // Setup the Route router.get('/', function (req, res) { Sid.findAll().then(function(users) { console.log(users);; }); // return a json response to angular res.json({ 'msg': users }); }); router.get('/sid', function (req, res) { // return a json response to angular res.json({ 'msg': "sid" }); }); // Expose the module module.exports = router; 

路由localhost:3000 /注册/ sid工作正常。 但是/注册正在放弃错误。 正如你可以在下面的错误文本中看到它显示连接正在尝试没有configuration提供的variables值(用户名,密码,主机)(configurationvariables正在工作,并获取罚款)。 我也不能在debugging时弄清楚。

 Unhandled rejection SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: YES) at Handshake._callback (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:51:20) at Handshake.Sequence.end (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24) at Handshake.ErrorPacket (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:8) at Protocol._parsePacket (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/protocol/Protocol.js:274:23) at Parser.write (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/protocol/Parser.js:77:12) at Protocol.write (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/protocol/Protocol.js:39:16) at Socket.<anonymous> (/Users/siddharthsrivastava/Documents/sites/express/LexStart/server/node_modules/mysql/lib/Connection.js:96:28) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20) 

请指导。

亚洲时报Siddharth

       

网上收集的解决方案 "通过Sequelize(Express / NodeJS)访问MySQL时出错"

dbConfig中没有用户名。 用你的configuration中的用户名replace用户。 "dbConfig": { "driver": "mysql", " user ": "root", ... var sequelize = new Sequelize( dbConfig.database, dbConfig. username ,

转到config.json并将用户更改为用户名并重新启动您的应用程序。 有时候没有错误,应用程序不起作用。

 "Lexstart": { "dbConfig": { "driver": "mysql", "username": "root", "database": "sid1", "password": "sid1", "host": "127.0.0.1", "port": "3306" } }