Sequelize create 不是函数?

Sequelize create not a function?(Sequelize create 不是函数?)
本文介绍了Sequelize create 不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 model.create,它说 model.create 不是一个函数.我四处搜索,似乎找不到任何解决方案.请注意,我通过 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
  });
}

附注一些建议

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.

这篇关于Sequelize create 不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)