日期时间字段不允许在请求中使用 NULL 值,但是查询通过 PostgreSQL 可以正常工作,如何修复?
我有一个工作应用程序,可以通过五个阶段跟踪申请人的招聘流程。我正在尝试调整它,以便用户可以手动输入日期,而不是让程序通过时间戳自动破译它。
我遇到的问题是,在某些情况下,日期应该为 NULL(因为这部分过程尚未完成。)当我直接在 PostgreSQL 中运行查询时,这没有问题,如下所示
但是,您将在这里看到,来自邮递员的具有空值的请求会呈现错误。我的后端代码具有与上面相同的查询,并且工作得很好,直到我尝试允许传递空值。为什么会这样??
我的后端代码如下
const { validationResult } = require("express-validator");
const { queryInstance } = require("../db/connection");
/*GET ALL DATE REQUEST*/
const getApplicants = async (req, res) => {
try {
const applicants = await queryInstance('SELECT * from applicants');
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*UPDATE STATUS DATA REQUEST*/
const updateApplicants = async (req, res) => {
try {
const applicant_id = req.params.applicant_id;
const {recruiterscreen_completion, backgroundcheck_completion, interview_completion, paperwork_completion, drugscreen_completion, testing_completion} = req.body
const applicants = await queryInstance(`UPDATE applicants SET recruiterscreen_completion = '${recruiterscreen_completion}', testing_completion='${testing_completion}', interview_completion='${interview_completion}', backgroundcheck_completion='${backgroundcheck_completion}', drugscreen_completion='${drugscreen_completion}', paperwork_completion='${paperwork_completion}' WHERE applicant_id = '${applicant_id}' returning *`);
res.json({ applicants })
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*ADD NEW APPLICANT REQUEST*/
const addApplicant = async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { store, first_name, last_name, position, recruiterscreen_completion,testing_completion,backgroundcheck_completion,interview_completion,drugscreen_completion,paperwork_completion, date_in } = req.body;
try {
const applicants = await queryInstance(`INSERT INTO applicants (store, first_name,last_name,position, recruiterscreen_completion,testing_completion,backgroundcheck_completion,interview_completion,drugscreen_completion,paperwork_completion, date_in) VALUES ('${store}', '${first_name}', '${last_name}', '${position}', '${recruiterscreen_completion}', '${testing_completion}', '${backgroundcheck_completion}','${interview_completion}','${drugscreen_completion}','${paperwork_completion}','${date_in}') RETURNING *`);
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*DELETE APPLICANT REQUEST*/
const deleteApplicant = async (req, res) => {
try {
const applicant_id = req.params.applicant_id;
const applicants = await queryInstance(`DELETE FROM applicants WHERE applicant_id = '${applicant_id}' RETURNING *`);
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
module.exports =
{deleteApplicant,getApplicants,updateApplicants,addApplicant}
I have a working application that tracks the hiring process of applicants through five stages. I am trying to adjust it so that the user can manually enter a date rather than having the program automatically decipher it via a timestamp.
The problem I am having is there are scenarios where a date should be NULL (because this part of the process is not yet completed.) This is no issue when I run the query directly in PostgreSQL as seen below
However, you will see here, that a request from postman with a null value renders an error. My back end code features the same exact query as above and was working just fine until I tried to allow the passing of a null value. Why would this be??
My backend code is below
const { validationResult } = require("express-validator");
const { queryInstance } = require("../db/connection");
/*GET ALL DATE REQUEST*/
const getApplicants = async (req, res) => {
try {
const applicants = await queryInstance('SELECT * from applicants');
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*UPDATE STATUS DATA REQUEST*/
const updateApplicants = async (req, res) => {
try {
const applicant_id = req.params.applicant_id;
const {recruiterscreen_completion, backgroundcheck_completion, interview_completion, paperwork_completion, drugscreen_completion, testing_completion} = req.body
const applicants = await queryInstance(`UPDATE applicants SET recruiterscreen_completion = '${recruiterscreen_completion}', testing_completion='${testing_completion}', interview_completion='${interview_completion}', backgroundcheck_completion='${backgroundcheck_completion}', drugscreen_completion='${drugscreen_completion}', paperwork_completion='${paperwork_completion}' WHERE applicant_id = '${applicant_id}' returning *`);
res.json({ applicants })
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*ADD NEW APPLICANT REQUEST*/
const addApplicant = async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { store, first_name, last_name, position, recruiterscreen_completion,testing_completion,backgroundcheck_completion,interview_completion,drugscreen_completion,paperwork_completion, date_in } = req.body;
try {
const applicants = await queryInstance(`INSERT INTO applicants (store, first_name,last_name,position, recruiterscreen_completion,testing_completion,backgroundcheck_completion,interview_completion,drugscreen_completion,paperwork_completion, date_in) VALUES ('${store}', '${first_name}', '${last_name}', '${position}', '${recruiterscreen_completion}', '${testing_completion}', '${backgroundcheck_completion}','${interview_completion}','${drugscreen_completion}','${paperwork_completion}','${date_in}') RETURNING *`);
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
/*DELETE APPLICANT REQUEST*/
const deleteApplicant = async (req, res) => {
try {
const applicant_id = req.params.applicant_id;
const applicants = await queryInstance(`DELETE FROM applicants WHERE applicant_id = '${applicant_id}' RETURNING *`);
res.json({ applicants });
} catch (err) {
console.error(err.message);
return res.status(500).send(err.message);
}
};
module.exports =
{deleteApplicant,getApplicants,updateApplicants,addApplicant}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在代码语句中使用引号 ' 的方式:
您用于 NULL 的 SQL 片段没有引号并且工作正常。您的代码应该根据输入处理引用。我对Reactjs一无所知,但你的代码也可能容易受到SQL注入的攻击,请检查你的代码是否使用了准备好的语句。如果是这样,为什么当您使用 NULL 值时会失败。
Because of the way you use quotes ' in your code statement:
The piece of SQL that you used for the NULL, is without the quotes and works fine. Your code should handle the quoting, depending on the input. I have no knowledge about Reactjs, but your code might vulnerable to SQL injection as well, please check if your code is using prepared statements. And if so, why it fails when you're using a NULL value.