brianschardt / node_rest_api_mysql

MIT License
248 stars 140 forks source link

user.getCompanies #17

Open eliecer2000 opened 5 years ago

eliecer2000 commented 5 years ago

Greetings, I really like your application, in fact I have learned a lot with the details of your explanation, but there is a function that I can not find, or I do not know where it comes from. and for future consultations of other tables know how to implement them.

[err, companies] = await to(user.getCompanies({include: [ {association: Company.Users} ] })); where “user.getCompanies” came from

Excuse my english, I’m supporting myself in google. Thank you.

RochMoreau commented 5 years ago

Hi ! Did you found the answer by yourself ? The getCompanies() function is defined by Sequelize, it is used to request datas from foreign key in the database. In your user.model.js you have

Model.associate = function(models){
        this.Companies = this.belongsToMany(models.Company, {through: 'UserCompany'});
    };

Using user.getCompanies() will return you a promise from which you can get all the companies linked to the user user.

Hope this helps