How to setup sequelize orm in nodejs
In the model :
var Sequelize = require('sequelize');
var sequelize_config = {
"username": process.env.DB_USERNAME || "root",
"password": process.env.DB_PASSWORD || "",
"pool": 200,
"database": "faasos_platform",
"host": "localhost",
"dialect": "mysql",
"dialectOptions": {
"multipleStatements": true
},
"logging": true,
"define": {
"timestamps": true,
"underscored": true
}
}
var sequeliz = new Sequelize(sequelize_config.database, sequelize_config.username, sequelize_config.password, sequelize_config);
module.exports = function (sequeliz, DataTypes) {
var auth = sequeliz.define("auth", {
device_id: {
type: DataTypes.STRING(50),
validate: {
notEmpty: true
}
},
customer_id: {
type: DataTypes.INTEGER,
validate: {
notEmpty: true
}
},
created_at: {
type: DataTypes.DATE,
validate: {
notEmpty: true
}
},
access_token: {
type: DataTypes.STRING(455),
validate: {
notEmpty: true
}
},
ip_address: {
type: DataTypes.STRING(20),
validate: {
notEmpty: true
}
},
is_active: {
type: DataTypes.INTEGER,
validate: {
notEmpty: true
}
}
}, {
classMethods: {
associate: function (models) {},
verify_user: function (req, callback) {
var d = new Date();
var sql = " Select customer_id From auth WHERE access_token =:access_token && is_active = '1' ";
sequelize.query(sql, {
replacements: {
access_token: req.access_token
},
raw: true
}).spread(function (data, metadata) {
callback(null, data);
}).catch(function (err) {
callback(err, null);
});
},
revoke_access_token: function (req, callback) {
var sql = "UPDATE auth SET is_active = 0 " +
"WHERE customer_id = :customer_id";
sequelize.query(sql, {
replacements: {
customer_id: req.id
},
raw: true
}).spread(function (data, metadata) {
callback(null, data);
}).catch(function (err) {
callback(err, null);
});
},
save_access_token_details: function (req, callback) {
var d = new Date();
var sql = "INSERT INTO auth (device_id, customer_id, created_at, access_token, ip_address, is_active)" +
"VALUES (:device_id, :cust_id, :created_at, :access_token, :ip_add, 1) ";
sequelize.query(sql, {
replacements: {
device_id: req.device_code ,
cust_id: req.id ,
created_at: d,
access_token: req.access_token,
ip_add: req.ip_add
},
raw: true
}).spread(function (data, metadata) {
callback(null, data);
}).catch(function (err) {
callback(err, null);
});;
}
},
tableName: 'auth',
timestamps: true,
underscored: true
});
return auth;
};
In my controller :
var models = require('../models/auth.js'); // the above model is saved in file auth.js
models.auth.verify_user(access_token, function (err, data) {
if (err) {
res.status(401).send({
'err': 'Unauthorized!'
});
}
if (data) {
models.revoke_access_token(user, function (err, data) {
if (err) {
res.status(401).send({
'err': 'Unauthorized!'
});
}
});
}
models.save_access_token_details(payload, function (err, data) {
if (err) {
res.status(401).send({
'err': 'Unauthorized!'
});
} else {
console.log(err, data);
res.send(data);
}
});
});
But each time it exists with the error ::
TypeError: Cannot call method 'verify_user' of undefined at SignStream. (/home/salim/Documents/proj/platform/oAuth/controllers/validate.js:25:19) at SignStream.EventEmitter.emit (events.js:95:17) at SignStream.sign (/home/salim/Documents/proj/platform/oAuth/node_modules/jsonwebtoken/node_modules/jws/lib/sign-stream.js:54:8) at SignStream. (/home/salim/Documents/proj/platform/oAuth/node_modules/jsonwebtoken/node_modules/jws/lib/sign-stream.js:37:12) at DataStream.g (events.js:180:16) at DataStream.EventEmitter.emit (events.js:92:17) at DataStream. (/home/salim/Documents/proj/platform/oAuth/node_modules/jsonwebtoken/node_modules/jws/lib/data-stream.js:20:12) at process._tickCallback (node.js:415:13) stream.js:94 throw er; // Unhandled stream error in pipe. ^ Error: read ECONNRESET at errnoException (net.js:901:11) at Pipe.onread (net.js:556:19)
Please help where am I going wrong ?? Why is the orm not able to recognize the function ????
Your problem is that models.auth
is undefined
after you initialize models
. Since models.auth
is undefined
, you cannot call its functions and cannot use its members.
auth
is a local variable inside module.exports
. Even though you return it, outside its scope you cannot use it.
If require
calls module.exports
, then your models
is the very same object as auth
, since you returned auth
, therefore models.verify_user
is existent in your code. However, I propose the following fix:
var models = {}; //creating an empty object which will hold the models
models.auth = require('../models/auth.js'); // the above model is saved in file auth.js
and then you will be able to use models.auth
.
上一篇: 错误连接ECONNREFUSED