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

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

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

问题

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

package.json

  1. {
  2. "name": "backend",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "type": "module",
  7. "scripts": {
  8. "test": "echo \"Error: no test specified\" && exit 1",
  9. "start": "nodemon index.js"
  10. },
  11. "keywords": [],
  12. "author": "",
  13. "license": "ISC",
  14. "dependencies": {
  15. "express": "^4.18.2",
  16. "mysql": "^2.18.1",
  17. "nodemon": "^2.0.20"
  18. }
  19. }

index.js

  1. import express from 'express';
  2. import mysql from "mysql";
  3. const app = express();
  4. const db = mysql.createConnection({
  5. host: "localhost",
  6. user: "root",
  7. password: "akdkfjdkfj;a",
  8. database: "online_sustainability_db"
  9. });
  10. app.use(express.json());
  11. app.get("/", (req, res) =>{
  12. res.json("Hello. You are connected to backend.");
  13. });
  14. app.get("/data", (req, res) =>{
  15. const query = "SELECT * FROM online_sustainability_db.idea_proposers";
  16. db.query(query, (err, data)=>{
  17. if(err)
  18. return res.json(err);
  19. else
  20. return res.json(data);
  21. })
  22. });
  23. app.post("/data", (req, res)=>{
  24. const q = "INSERT INTO idea_proposers (`last_name`, `first_name`, `account_no`, `github_repository_link`, `submission_id`) VALUES (?, ?, ?, ?, ?)";
  25. const last_name = req.body.last_name;
  26. const first_name = req.body.first_name;
  27. const account_no = req.body.account_no;
  28. const github_link = req.body.github_repository_link;
  29. const submission_id = req.body.submission_id;
  30. db.query(q, [last_name, first_name, account_no, github_link, submission_id], (err, data)=>{
  31. if(err)
  32. return res.json(err);
  33. else
  34. return res.json("Provided data were recorded successfully.");
  35. });
  36. });
  37. app.listen(8800, ()=>{
  38. console.log("Connected to backend!");
  39. });

以下图片来自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

  1. {
  2. "name": "backend",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "type": "module",
  7. "scripts": {
  8. "test": "echo \"Error: no test specified\" && exit 1",
  9. "start": "nodemon index.js"
  10. },
  11. "keywords": [],
  12. "author": "",
  13. "license": "ISC",
  14. "dependencies": {
  15. "express": "^4.18.2",
  16. "mysql": "^2.18.1",
  17. "nodemon": "^2.0.20"
  18. }
  19. }

index.js

  1. import express from 'express';
  2. import mysql from "mysql";
  3. const app = express();
  4. const db = mysql.createConnection({
  5. host: "localhost",
  6. user: "root",
  7. password: "akdkfjdkfj;a",
  8. database: "online_sustainability_db"
  9. });
  10. app.use(express.json());
  11. app.get("/", (req, res) =>{
  12. res.json("Hello. You are connected to backend.");
  13. });
  14. app.get("/data", (req, res) =>{
  15. const query = "SELECT * FROM online_sustainability_db.idea_proposers";
  16. db.query(query, (err, data)=>{
  17. if(err)
  18. return res.json(err);
  19. else
  20. return res.json(data);
  21. })
  22. });
  23. app.post("/data", (req, res)=>{
  24. const q = "INSERT INTO idea_proposers (`last_name`, `first_name`, `account_no`, `github_repository_link`, `submission_id`) VALUES (?, ?, ?, ?, ?)";
  25. const last_name = req.body.last_name;
  26. const first_name = req.body.first_name;
  27. const account_no = req.body.account_no;
  28. const github_link = req.body.github_repository_link;
  29. const submission_id = req.body.submission_id;
  30. db.query(q, [last_name, first_name, account_no, github_link, submission_id], (err, data)=>{
  31. if(err)
  32. return res.json(err);
  33. else
  34. return res.json("Provided data were recorded successfully.");
  35. });
  36. });
  37. app.listen(8800, ()=>{
  38. console.log("Connected to backend!");
  39. });

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:

确定