Sequelize hasOne关联关联方法不是一个函数?

时间:2016-06-30 03:44:34

标签: node.js postgresql orm sequelize.js

我正在使用Node和Sequelize ORM的后端。正在使用的数据库是PostgreSQL。我之前在代码中使用了hasOne(),并且在我之前使用它时,createAssociation()关系中的所有getAssociation()hasOne()方法都运行得很好。但由于某种原因,它现在给我一个问题。

index.js

const Connection = new Sequelize(
  'postgres',
  'postgres',
  null,
  {
    dialect: 'postgres',
    host: '192.168.99.100',
    port: '5432',
  }
);

const ModelSeven = Connection.define('modelSeven', ModelSevenSchema),
  ModelFive = Connection.define('modelFive', ModelFiveSchema),
  ModelOne = Connection.define('modelOne', ModelOneSchema),
  ModelSix = Connection.define('modelSix', ModelSixSchema),
  ModelThree = Connection.define('modelThree', ModelThreeSchema),
  ModelFour = Connection.define('modelFour', ModelFourSchema),

ModelOne.hasOne(ModelThree); // Defines hasOne() relationship
ModelOne.hasOne(ModelFour); // Defines hasOne() relationship

ModelSix.belongsToMany(ModelOne, {through: 'Pivot'});
ModelOne.belongsToMany(ModelSix, {through: 'Pivot' });
ModelOne.belongsTo(ModelSeven);

ModelThree.hasMany(ModelFive);
ModelFour.hasMany(ModelFive);

export default Connection;

utilize.js

import DB from './somePathToIndex.js';

  //GraphQL resolve function
  async resolve (root, args, context) {
      //creates ModelOne 
      var ModelOne = await DB.models.ModelOne.create({
        value: args.value,
        valueTwo: args.valueTwo
      });
      // attempts to createModelThree/createModelFour from hasOne() relationship
      var ModelThree = await ModelOne.createModelThree({});
      var ModelFour = await ModelOne.createModelFour({});

      // Maps through value array finding the modelFive with and id and adding 
      // it to modelThree via hasMany() relationship
      args.value.map(async (id, key) => {
        var ModelFive = await DB.models.ModelFive.findAll({
          where: {
            id: id
          }
        });

        await ModelThree.addModelFive(ModelFive);
      });

      // Maps through value array finding the modelFive with and id and adding 
      // it to modelFour via hasMany() relationship
      args.valueTwo.map(async (id, key) => {
        var ModelFive = await DB.models.ModelFive.findAll({
          where: {
            id: id
          }
        });

        await ModelFour.addModelFive(ModelFive);
      });

      return ModelOne;
  }

问题是当节点命中行时:

  var ModelThree = await ModelOne.createModelThree({});
  var ModelFour = await ModelOne.createModelFour({});

给我以下错误:

"errors": [
   {
     "message": "ModelOne.createModelThree is not a function",
     "locations": [
       {
         "line": 38,
         "column": 3
       }
     ]
   }
 ]

此错误表示hasOne()关系方法create[Association]()不是函数。我不知道为什么会这样。如果你知道为什么请告诉我。

0 个答案:

没有答案