提供一个在Node.js中解决ER_BAD_NULL_ERROR的方法,用于mysql的crud操作。

huangapple go评论59阅读模式
英文:

Tell me a fix for ER_BAD_NULL_ERROR in Node.js, mysql crud operation

问题

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

package.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"
  }
}

index.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应用程序。这是我遇到的错误。请帮助我修复它。

提供一个在Node.js中解决ER_BAD_NULL_ERROR的方法,用于mysql的crud操作。

这是我试图在其中发布数据的表的描述。

提供一个在Node.js中解决ER_BAD_NULL_ERROR的方法,用于mysql的crud操作。

我尝试了一些语法更改并多次运行代码。好吧,它没有起作用。我甚至在网上搜索了一些资源,但找不到类似的内容。

英文:

I am an absolute novice at Node.js. So, as I'm learning, I ran into this problem.
I am adding codes from backend for CRUD (This index.js may seem incomplete, because I faced the problem halfway and then started seeking the solution.)

package.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"
}
}

index.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!");
});

The following image is from postman application. This is the error I am getting. Please, help me fixing it.
提供一个在Node.js中解决ER_BAD_NULL_ERROR的方法,用于mysql的crud操作。

This is the description of the table I am trying to post the data in.
提供一个在Node.js中解决ER_BAD_NULL_ERROR的方法,用于mysql的crud操作。

I tried doing some syntactical change and running the code several times. Well, it didn't work. I even looked for resources online, but I couldn't find any similar.

答案1

得分: 1

请在需要发送JSON数据时从Postman中选择JSON。目前,您正在将数据发送为文本。

英文:

Kindly select the JSON from Postman whenever you want to send the JSON data. currently, you're sending data as a text.

huangapple
  • 本文由 发表于 2023年1月6日 15:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75028021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定