gpt4 book ai didi

node.js - SequelizeDatabaseError : null value in column "name" violates not-null constraint (NodeJS + Sequelize + Postgres)

转载 作者:行者123 更新时间:2023-12-03 22:26:24 25 4
gpt4 key购买 nike

我正在尝试在我的表 'students' 中插入一个学生,但出现以下错误:

Executing (default): INSERT INTO "students" ("id","created_at","updated_at") VALUES (DEFAULT,$1,$2) RETURNING *; (node:6582) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: null value in column "name" violates not-null constraint



我正在使用 Node.js + Sequelize (Postgres)。

这是我的代码:

Student.js(模型)

import Sequelize, { Model } from 'sequelize';

class Student extends Model {
static init(sequelize) {
super.init(
{
name: Sequelize.STRING,
email: Sequelize.STRING,
age: Sequelize.INTEGER,
weight: Sequelize.DECIMAL(10, 2),
height: Sequelize.DECIMAL(10, 2),
},
{
sequelize,
}
);
}
}

export default Student;


StudentController.js( Controller )

import Student from '../models/Student';

class StudentController {
async store(res, req) {
const student = await Student.create(req.body);

return res.json(student);
}
}

export default new StudentController();


route.js(路由)

import { Router } from 'express';

import StudentController from './app/controllers/StudentController';

const routes = new Router();

routes.post('/students', StudentController.store);

export default routes;


我正在使用 Insomnia 通过 POST 发送数据。
enter image description here

任何想法?

最佳答案

我能想到这里出错的两件事

1. StudentController.js

class StudentController {
// Following should be `(req, res)`
async store(res, req) { // <-- it should be (req, res)
const student = await Student.create(req.body);

return res.json(student);
}
}

2. 应用设置

由于您没有共享用于启动快速应用程序的代码,如下所示
const express = require("express")
const app = express()

您可能错过了使用解析 json 正文的正文解析器中间件。

关于node.js - SequelizeDatabaseError : null value in column "name" violates not-null constraint (NodeJS + Sequelize + Postgres),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com