最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sequelize.js - Sequelize CLI access from one model to another - Stack Overflow

programmeradmin5浏览0评论

I use sequelize-cli migration scripts to create models I have created models 'user' and 'user-role' I want to access to model user_role from the file user This how my directory structure looks like: [folders structure][1] [1]: .png in my user.js file i write

const {user_role} = require('./index')

I suppose to get a user_role class extended from Model class. In user_role class i defined static async function grantRole(user, role) I want to use this function in user.js class. So i write:

await user_role.grantRole(newUser, userRoles.admin)

but in runtime I get an error 'grantRole is not a function'

However I can use this function from other js files that are not in models folders and do not represent a model for db

const {user, user_role} = require('../db/models/index')
const {userRoles} = require('../db/middleware')
try {
    const regUser = await user.register(userCreds.UserName, userCreds.UserMail, userCreds.UserPass) 
    await user_role.grantRole(regUser, userRoles.admin)      
} catch(error) {
    console.log(error.message)
}

What do I do wrong?

发布评论

评论列表(0)

  1. 暂无评论