Home > Net >  Sequelize - Model.create is not a function / Model.build is not a function
Sequelize - Model.create is not a function / Model.build is not a function

Time:10-18

I'm creating an app with Node.js, Sequelize, Mysql, Express. I'm very new to this, sorry if this question is stupid.

I have two models with a one to many relation : User and Token (a user has multiple tokens, a token has one user).

Here are the files :

models/user.js

const { Sequelize, DataTypes } = require('sequelize');
const Token = require('./token')
const db = require('../db/sequelize'),
      sequelize = db.sequelize,
      jwt = require('jsonwebtoken')

// Model for a user (player)

const User = sequelize.define('User', {
    // All the columns
})
    
User.gAuth = async function(user) {
    const token = jwt.sign({id: user.id.toString() },
        'password',
        {
            expiresIn: '7 days'
        })

    console.log(token)
    console.log(Token)

    const newToken = Token.create({
        token,
        user: this.id
    })

    console.log(newToken)

    await newToken.save()

    return token

}

module.exports = User

It's on "const newToken = Token.create({...})" that I have my error. It says : "Token.create is not a function". I tried with build, I had : "TypeError: Token.build is not a function".

models/token.js

const { Sequelize, DataTypes } = require('sequelize');
const db = require('../db/sequelize'),
    sequelize = db.sequelize
const User = require('./user')

const Token = sequelize.define('Token', {
    // All the columns
})

User.hasMany(Token, {
    as: 'tokens'
})

Token.belongsTo(User, {
    foreignKey: 'UserId',
    as: 'user'
})

module.exports = Token

Can you please help me understands what I'm doing wrong ? All the answers I've found were about bad export of the Model, but I don't see my error.

Thank you !

EDIT :

When I console.log(sequelize.models) in models/token.js it returns : { User: User, Token: Token }

However ! When I console.log(sequelize.models) in models/user.js it retuns : { User: User }

CodePudding user response:

Try this:

const { Sequelize, DataTypes } = require('sequelize');
const db = require('../db/sequelize'),
      sequelize = db.sequelize,
      jwt = require('jsonwebtoken')
const Token = require('./token')(sequelize, Sequelize)

Edit:

As per the docs instead of importing Token directly try grabbing the token off the sequelize instance like so:

const db = require('../db/sequelize'),
   sequelize = db.sequelize,
   jwt = require('jsonwebtoken')
const token = sequelize.models.Token

CodePudding user response:

OKAY ! I found the thing I was doing wrong : structuring my files.

My function gAuth shouldn't have been on models/user.js but on models/token.js. Leaving it on user.js caused a circular dependency which was causing the bug.

I've removed gAuth from user.js, moved it to token.js and removed the require('./token') from user.js

I hope this can help some people with the same problem !

Thanks a lot to @about14sheep for helping me out.

  • Related