NodeJS 序列化与别名的关联不存在

y4ekin9u  于 2023-03-29  发布在  Node.js
关注(0)|答案(1)|浏览(125)

我知道有一大堆这样的问题,我尝试了许多解决方案,我可以找到,但似乎没有为我工作。
我有两个模型,Player和PlayerSkill。
玩家:

import Sequelize from 'sequelize';
import database from '../index';

const Player = database.define(
     'player',
   {
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      allowNull: false,
      primaryKey: true,
    },
    name: {
      type: Sequelize.STRING(200),
      allowNull: false,
    },
    position: {
      type: Sequelize.STRING(200),
      allowNull: false,
    },
  },
  {
    timestamps: false,
  }
);

Player.associate = models => {
  models.Player.hasMany(models.PlayerSkill, {
    as: 'playerskills',
    foreignKey: 'playerId',
  });
};

export default Player;

我的PlayerSkill模型:

import Sequelize from 'sequelize';
import database from '../index';

const PlayerSkill = database.define(
  'playerSkill',
  {
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      allowNull: false,
      primaryKey: true,
    },
    skill: {
      type: Sequelize.STRING(200),
      allowNull: false,
    },
    value: {
      type: Sequelize.INTEGER,
    },
    playerId: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
  },
  {
    timestamps: false,
  }
);

PlayerSkill.associate = models => {
  models.PlayerSkill.belongsTo(models.Player, {
    foreignKey: 'playerId',
    allowNull: false,
  });
};

export default PlayerSkill;

我需要以如下方式获取数据:

{
    "id": 1,
    "name": "player name 2",
    "position": "midfielder",
    "playerSkills": [
        {
            "id": 1,
            "skill": "attack",
            "value": 60,
            "playerId": 1
        },
        {
            "id": 2,
            "skill": "speed",
            "value": 80,
            "playerId": 1
        }
    ]
}

下面是我的fetch函数:

// Create new player
  await Player.create(req.body)
    .then(() => {
      PlayerSkill.bulkCreate(skills);
    })
    .then(() => Player.findAll({ include: ['playerskills'] }))
    .then(result => console.log(result))
    .catch(err => {
      res.status(400).send({
        message: err.message || 'Error occurred while creating player',
      });
    });

Postman 要求:

{
    "name": "player1",
    "position": "defender",
    "playerSkills": [
        {
            "skill": "strength",
            "value": 90,
            "playerId": 1
        },
        {
            "skill": "speed",
            "value": 90,
            "playerId": 1
        }
    ]
}

我不明白我在这里做错了什么。我一直得到以下错误:

{
    "message": "Association with alias \"playerskills\" does not exist on player"
}
pcrecxhr

pcrecxhr1#

你应该把Player模块中hasMany的直接调用转换成associate方法,就像你在PlayerSkill模型模块中一样。不要忘记从Player模块中删除PlayerSkill导入行。

model/player.js

import Sequelize from 'sequelize';
import database from '../index';

const Player = database.define(
  'player',
  {
...
);

Player.associate = models => {
  Player.belongsTo(models.PlayerSkill, {
    foreignKey: 'playerId',
    onDelete: 'cascade'
  });
};

确保在两个配对的关联中有相同的foreignKey选项。并使用来自一个已经定义的模块index的模型:

import { Player, PlayerSkill } from '../../db/model/index';

export default async (req, res) => {
  try {
    const players = await Player.findAll({ include: PlayerSkill });
...

相关问题