续集创建没有功能? [英] Sequelize create not a function?

查看:98
本文介绍了续集创建没有功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用model.create,也就是说model.create不是函数.我四处搜寻,似乎找不到任何rmesolutions.请注意,我正在通过babel在节点中使用es6导入/导出.

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 es6 imports / exports in node via babel.

model.js

'use strict';
export default (sequelize, DataTypes) => {
  let attachments = sequelize.define('attachments', {
    type: DataTypes.STRING,
    category: DataTypes.STRING,
    value: DataTypes.STRING,
  }, {});
  attachments.associate = (models) => {
  };
  return attachments;
};

控制器

import attachments from '../../../models/attachments'
import to_attachments from '../../../models/to_attachments'
import AWSService from '../../utils/awsS3Api'

export async function createPhoto(ctx) {

 ... 

  try {
      let attachment = await attachments.create({
        type: 'image',
        category: imageCategory,
        value: data.location
      });
..etc

我在做什么错了?

推荐答案

如果通过函数声明模型:

If you declare model via function:

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

然后您应该使用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.一些建议

1)用静态处理的大写字母命名模型变量.

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)单数不是Users,而是User.

2) Singular not Users but User.

这篇关于续集创建没有功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆