告诉我Node.js中ER_BAD_NULL_ERROR的修复方法,mysql crud操作

jaql4c8m  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(220)

我是一个Node.js的新手,所以,当我学习的时候,我遇到了这个问题。我从后端为CRUD添加代码(这个index.js可能看起来不完整,因为我中途面对了这个问题,然后开始寻找解决方案。)

软件包. json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "mysql": "^2.18.1",
    "nodemon": "^2.0.20"
  }
}

索引. js

import express from 'express';
import mysql from "mysql";

const app = express();

const db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "akdkfjdkfj;a",
    database: "online_sustainability_db"
});

app.use(express.json());   

app.get("/", (req, res) =>{
    res.json("Hello. You are connected to backend.");
});

app.get("/data", (req, res) =>{
    const query = "SELECT * FROM online_sustainability_db.idea_proposers";
    db.query(query, (err, data)=>{
        if(err)
            return res.json(err);
        else 
            return res.json(data);
    })
});

app.post("/data", (req, res)=>{
    const q = "INSERT INTO idea_proposers (`last_name`, `first_name`, `account_no`, `github_repository_link`, `submission_id`) VALUES (?, ?, ?, ?, ?)";
    
    const last_name = req.body.last_name;
    const first_name = req.body.first_name; 
    const account_no = req.body.account_no; 
    const github_link = req.body.github_repository_link; 
    const submission_id = req.body.submission_id;
    
    
    db.query(q, [last_name, first_name, account_no, github_link, submission_id], (err, data)=>{
        if(err)
            return res.json(err);
        else 
            return res.json("Provided data were recorded successfully.");
    });
});

app.listen(8800, ()=>{
    console.log("Connected to backend!");
});

下面的图像是从 Postman 应用程序。这是我得到的错误。请帮助我修复它。

这是我尝试将数据发布到.

中的表的描述
我试着做一些语法上的改变并运行代码几次,但是没有成功,我甚至在网上寻找资源,但是我找不到任何类似的。

gjmwrych

gjmwrych1#

请选择JSON从 Postman 每当你想发送JSON数据。目前,你发送数据作为一个text

相关问题