F
F
Flasher2016-01-14 12:32:44
JavaScript
Flasher, 2016-01-14 12:32:44

How to export a function?

Tell me how to properly export the search_all_user function?

module.exports = function (sequelize, Sequelize, DataTypes) {
    var User = sequelize.define('user', {
            // тут поля модели    
    }, {
        classMethods: {
            search_all_user: function() {
                User.findOne({where: {username: 'coderock'}}).then(function(user){
                   console.log(user.username);
                });
            }
        }
    });
    return User;
};

I want to pass the search_all_user function here:
exports.index = function (request, response, next) {
    // сюда передать функцию search_all_user
    response.render('user_signup', {title: 'Регистрация пользователя'});
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhukov, 2016-01-14
@MrCheater

require('search_all_user');
Well, in general, if you write like that, there will be problems with testing. You will not be able to lock the function in tests. It's best to wrap all functions in one object and export it exclusively.
And it’s also better to immediately reduce all work with the database into one module and pull the functions from there - otherwise it’s also fraught with problems when testing and when changing the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question