Sequelize创建不是一个功能?

时间:2018-02-19 23:28:47

标签: javascript node.js sequelize.js koa

我正在尝试使用model.create,并且它说model.create不是一个函数。我google了一下,似乎无法找到任何rmesolutions。请注意,我通过babel在节点中使用es6导入/导出。

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果您通过函数声明模型:

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)使用大写字母命名模型变量,处理静态类。

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

2)单数不是Users而是User