如何调用导出函数(node.js / sequelize)

时间:2016-01-14 08:49:45

标签: javascript node.js express

如何调用函数(search_all_user)进行导出(node.js / sequelize)

module.exports = function (sequelize, Sequelize, DataTypes) {
var User = sequelize.define('user', {

    id: {type: Sequelize.INTEGER, unique: true, primaryKey: true, autoIncrement: true},
    username: {type: Sequelize.STRING(32), unique: true, allowNull: false},

}, {
    classMethods: {
        search_all_user: function() {
            User.findOne({where: {username: 'coderock'}}).then(function(user){
               console.log(user.username);
            });
        }
    }
});

return User;
};

控制器:

model  = require('./../models/user.js');

exports.index = function (request, response, next) {
// how to insert function () search_all_user?
response.render('user_signup', {title: 'Регистрация пользователя'});
};

1 个答案:

答案 0 :(得分:0)

在用户模型的类方法中定义的

search_all_users。因此,您可以在需要模型后在控制器中调用它。这是一个例子:

 var model  = require('./../models/user.js');

exports.index = function (request, response, next) {
model.search_all_users.then(...);
};