I am trying to start using Sequelize. I am facing some errors though figured and cleared some of them but this one I am unable to figure out.
Project Structure:
Project
-Client
-Server
--models
---index.js
---Sid.js
--router
---routes
----signup.js
---index.js
app.js
my config varriable:
"Lexstart": {
"dbConfig": {
"driver": "mysql",
"user": "root",
"database": "sid1",
"password": "sid1",
"host": "127.0.0.1",
"port": "3306"
}
}
My models/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;
models/Sid.js
module.exports = function(sequelize, DataTypes) {
var Sid = sequelize.define("Sid", {
username: DataTypes.STRING,
password: DataTypes.STRING
});
return Sid;
};
my router/index.js
/**
* The Index of Routes
*/
module.exports = function (app) {
// The signup route
app.use('/signup', require('./routes/signup'));
}
my router/routes/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;
The route localhost:3000/signup/sid is working fine. But the /signup is giving up error. As you can see in the error text below it showing that connection is being tried without the config supplied variable value (username, password, host)(The config variable are working and fetching fine). I can't figure this out while debugging also.
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)
Please guide.
Siddharth
There is no username in dbConfig.
Replace user with username in your config.
"dbConfig": {
"driver": "mysql",
"user": "root",
...
var sequelize = new Sequelize(
dbConfig.database,
dbConfig.username,
Go to config.json and change user to username and restart your app. Sometimes there are no errors and app just doesn't work.
"Lexstart": {
"dbConfig": {
"driver": "mysql",
"username": "root",
"database": "sid1",
"password": "sid1",
"host": "127.0.0.1",
"port": "3306"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With