sequelize rest api 多次更新布尔值

sequelize rest api updating boolean more than one time

我正在使用 node.js 开发应用程序并表达 REST API 来管理 sqlite 数据库后端。我正在使用 sequelize 作为 orm。到目前为止一切正常,但我发现了一个我无法解决的奇怪错误。我有一个处理基本游戏设置的游戏端点,比如它是哪个游戏,游戏的哪个变体,以及如果游戏获胜或需要下一个玩家执行操作的标志。

所以 this won 和 nextPlayerNeeded 是您已经猜到的布尔标志。我的模型如下所示:

models/game.js:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Game = sequelize.define(
    'Game',
    {
      game: DataTypes.STRING,
      variant: DataTypes.STRING,
      inGame: DataTypes.STRING,
      outGame: DataTypes.STRING,
      won: DataTypes.BOOLEAN,
      nextPlayerNeeded: DataTypes.BOOLEAN
    },
    {}
  );
  Game.associate = function(models) {};
  return Game;
};

对应的迁移脚本为,

migrations/game.js:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Games', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      game: {
        allowNull: false,
        type: Sequelize.STRING
      },
      variant: {
        allowNull: false,
        type: Sequelize.STRING
      },
      inGame: {
        allowNull: true,
        type: Sequelize.STRING
      },
      outGame: {
        allowNull: true,
        type: Sequelize.STRING
      },
      won: {
        allowNull: false,
        type: Sequelize.BOOLEAN
      },
      nextPlayerNeeded: {
        allowNull: false,
        type: Sequelize.BOOLEAN
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Games');
  }
};

我的 CRUD 控制器 controller/gameController.js 看起来像这样:

const { Game } = require('../models');
const fs = require('fs');

module.exports = {
  getAllGames(req, res) {
    return Game.findAll({
      attributes: [
        'id',
        'game',
        'variant',
        'inGame',
        'outGame',
        'won',
        'nextPlayerNeeded'
      ]
    })
      .then(games => res.status(200).send(games))
      .catch(err => res.status(400).send(err));
  },
  getSpecificGame(req, res) {
    return Game.findByPk(req.params.id, {
      attributes: [
        'id',
        'game',
        'variant',
        'inGame',
        'outGame',
        'won',
        'nextPlayerNeeded'
      ]
    }).then(game => {
      if (!game) {
        return res.status(404).send({
          message: `Game with id ${req.params.id} not found`
        });
      }
      return res.status(200).send(game);
    });
  },
  createGame(req, res) {
    return Game.create({
      game: req.body.game,
      variant: req.body.variant,
      inGame: req.body.inGame,
      outGame: req.body.outGame,
      won: false,
      nextPlayerNeeded: false
    })
      .then(game => res.status(201).json(game))
      .catch(err => res.status(400).send(err));
  },
  updateGame(req, res) {
    return Game.findByPk(req.params.id, {
      attributes: [
        'id',
        'game',
        'variant',
        'inGame',
        'outGame',
        'won',
        'nextPlayerNeeded'
      ]
    })
      .then(game => {
        if (!game) {
          return res.status(404).send({
            message: `Game with id ${req.params.id} not found`
          });
        }
        return game
          .update({
            game: req.body.game || game.game,
            variant: req.body.variant || game.variant,
            inGame: req.body.inGame || game.inGame,
            outGame: req.body.outGame || game.outGame,
            won: req.body.won || game.won,
            nextPlayerNeeded: req.body.nextPlayerNeeded || game.nextPlayerNeeded
          })
          .then(() => res.status(200).send(game))
          .catch(err => res.status(400).send(err));
      })
      .catch(err => res.status(400).send(err));
  },
  deleteGame(req, res) {
    return Game.findByPk(req.params.id, {
      attributes: [
        'id',
        'game',
        'variant',
        'inGame',
        'outGame',
        'won',
        'nextPlayerNeeded'
      ]
    })
      .then(game => {
        if (!game) {
          return res.status(404).send({
            message: `Game with id ${req.params.id} not found`
          });
        }
        return game
          .destroy()
          .then(() => res.status(204).send())
          .catch(err => res.status(400).send(err));
      })
      .catch(err => res.status(400).send(err));
  }
};

现在是有趣的部分。我正在使用 insomnia 作为测试客户端,我可以更新实例化的游戏对象,说它赢了 (true) 并且它有 nextPlayerNeeded (true)。所以 won 永远不应该再次更新为 false,因为如果赢了,游戏就结束了,nextPlayerNeeded 标志在切换到下一个玩家后应该再次为 false。但是将它们中的任何一个更新为 true 使得无法再次更新为 false。这是为什么?为什么我只能将这些字段更新一次?

例如,这是来自我将标志更新为 true 的请求的响应:

{
  "id": 2,
  "game": "X01",
  "variant": "301",
  "inGame": "Straight",
  "outGame": "Double",
  "won": false,
  "nextPlayerNeeded": true,
  "updatedAt": "2020-01-21T10:47:23.767Z"
}

我可以随时更新所有 string 标志,但不能更新布尔值。

如果能得到任何提示,我将不胜感激。谢谢

我确实重构了我的项目以使用 GraphQL Apollo 服务器和客户端而不是 REST Api。尽管在这两种变体中我都使用 sequelize,但 GraphQL 会根据需要多次更新它。因此,请考虑这个未答复但已关闭的问题。