Sequelize create not a function?

前端 未结 1 1572
一整个雨季
一整个雨季 2021-01-28 12:40

I am trying to use model.create and it\'s saying that model.create is not a function. I googled around and couldn\'t seem to find any rmesolutions. Please note that I\'m using e

相关标签:
1条回答
  • 2021-01-28 13:13

    If you declare model via function:

    export default (sequelize, DataTypes) => {
      ...
    };
    

    Then you should import it with sequelize.import

    const Attachment = sequelize.import('../../../models/attachments')
    
    export async function createPhoto(ctx) {
      const attachment = await Attachment.create({
        type: 'image',
        category: imageCategory,
        value: data.location
      });
    }
    

    P.S. Some recommendations

    1) Name model variables with capital letter you deal with static classes.

    const user = await User.findById(123)
    const users = await User.findAll({where: {id: [1,2,3]}})
    

    2) Singular not Users but User.

    0 讨论(0)
提交回复
热议问题