【问题标题】:Not able to execute post request using node js ,mysql and sequelize library无法使用节点 js、mysql 和 sequelize 库执行发布请求
【发布时间】:2018-07-19 04:29:14
【问题描述】:

我正在使用 node.js、Mysql 和 Sequelize ORM Library 为简单的 Todo App 制作 api。基本上有 2 个表 Todos 和 TodoItems。这两张表的关系是

1) 一个 Todo 可以有多个 TodoItems 。 2) 一个 TodoItem 只能有一个 TodoId。

所以根据第二个关系,我在 TodoItems 表中创建一个外键,并为在 TodoItems 表中输入一个 TodoItem 发出一个发布请求。

这是我的控制器函数,用于在表格中创建和输入 todoitem

const TodoItem = require('../models').TodoItem;

module.exports = {
  create(req, res) {

    console.log(req.params.todo_id)
    return TodoItem
      .create({
        content: req.body.content,
        todoId: req.params.todo_id,

      })
      .then(todoItem => res.status(201).send(todoItem))
      .catch(error => res.status(400).send(error));
  },
};

这是我创建 TodoItems 表的迁移文件

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('TodoItems', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      content: {
        type: Sequelize.STRING
      },
      complete: {
        type: Sequelize.BOOLEAN
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },

      todoId:{

        type:Sequelize.INTEGER,
        onDelete: 'CASCADE',
        allowNull:false,

        references:{

          model:'Todos',
          key: 'id',
          as: 'todoId',
        }

      },
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('TodoItems');
  }
};

同样的路由器功能是

var express = require ('express');
var router = express.Router();
var model = require('../models/index');
var db = require('../models');
const todosItemsController = require('../controllers').todoItems;


router.post('/:todo_id', todosItemsController.create);


module.exports = router;

所以api链接有点像这样

localhost:4006/todoitems/1

但是当我在邮递员中运行这个 api 进行测试时,它给出了这个错误

{
    "name": "SequelizeDatabaseError",
    "parent": {
        "code": "ER_NO_DEFAULT_FOR_FIELD",
        "errno": 1364,
        "sqlState": "HY000",
        "sqlMessage": "Field 'todoId' doesn't have a default value",
        "sql": "INSERT INTO `TodoItems` (`id`,`content`,`complete`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'first todo item inside first todo',false,'2018-02-08 14:38:10','2018-02-08 14:38:10');"
    },
    "original": {
        "code": "ER_NO_DEFAULT_FOR_FIELD",
        "errno": 1364,
        "sqlState": "HY000",
        "sqlMessage": "Field 'todoId' doesn't have a default value",
        "sql": "INSERT INTO `TodoItems` (`id`,`content`,`complete`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'first todo item inside first todo',false,'2018-02-08 14:38:10','2018-02-08 14:38:10');"
    },
    "sql": "INSERT INTO `TodoItems` (`id`,`content`,`complete`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'first todo item inside first todo',false,'2018-02-08 14:38:10','2018-02-08 14:38:10');"
}

由于我验证了 todoId 不为空,因此它不能取空值,但我在 api 链接中传递了 todoId,我还编写了应该从 api 链接获取 todoId 并将该值分配给 todoId 列的代码桌子。

我无法理解为什么会出现此错误。 还有一件事我无法理解,当我在 TodoItems 中也有 todoId 列时,但在错误中我能够看到 TodoItem 表的所有列,但看不到 TodoId,因为它是写的

“插入TodoItems (id,content,complete,createdAt,updatedAt)”。

为什么缺少 TodoId 列。看不懂

请帮忙提前谢谢!!

【问题讨论】:

    标签: node.js express sequelize.js sequelize-cli


    【解决方案1】:

    我没有正确阅读文档,这就是我没有声明的原因

    tod​​oId: { 类型:DataTypes.BIGINT, 允许空:假, 参考: { 模型:'待办事项', 键:'id' } }, 在 todoItems 模型中

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 2019-05-18
      • 1970-01-01
      • 2023-02-19
      • 2018-01-15
      • 2022-11-09
      • 2019-11-14
      • 2018-07-30
      • 1970-01-01
      相关资源
      最近更新 更多