NodeJS 服务器因SQL而崩溃[已关闭]

hwamh0ep  于 2023-03-17  发布在  Node.js
关注(0)|答案(1)|浏览(196)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

3天前关闭。
Improve this question
mysql将崩溃express node.js服务器
我有使用MySQL的express node.js服务器,用户发送数据,服务器将该数据与数据库中该数据的价格添加到数据库中,但有时价格在数据库中不存在,服务器将崩溃如何修复它?

C:\Users\PC\Desktop\project 
main\server\node_modules\mysql\lib\protocol\Parser.js:437      throw 
err; // Rethrow non-MySQL errors

我的代码

app.post("/api/post/secorit", (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
const secoritreq = req.body.noejam;
const bodyjamreq = req.body.bodyjam;
const meterjamreq = req.body.meterjam;
const ipclient = req.body.ip;
const zekhamat = req.body.zekhamat;
const noejamSend = req.body.noejamsend;
const diamond = req.body.diamond;
const noeSecorit = req.body.noesecorit;
const metrazh = req.body.metrazh;
const olgobori = req.body.olgobori;

con.query(
 "SELECT `gheimatNahaei` FROM `listgheimatsecorit` WHERE noeShishe = 
  '" +
  noejamSend +
  "'  AND `zekhamat` = '" +
  zekhamat +
  "' ",
  function (err, resultmohasebeh) {
  
  const pricejamdatabass = (results = JSON.parse(
    JSON.stringify(resultmohasebeh)
  ));
  const pricevahed = pricejamdatabass[0].gheimatNahaei;
  const pricejam = pricejamdatabass[0].gheimatNahaei * metrazh;
  const secorit =
    "INSERT INTO `tempfactor` (`noejam`, `bodyjam` 
  ,`pricevahed`,`pricejam`, `meterjam`, `ip`) VALUES ('" +
    secoritreq +
    "', '" +
    bodyjamreq +
    "','" +
    pricevahed +
    "','" +
    pricejam +
    "', '" +
    meterjamreq +
    "', '" +
    ipclient +
    "')";
    con.query(secorit, function (err, result) {
     if (err) throw err;
    });
 
    }

    );

    res.sendStatus(200);
    });
dffbzjpn

dffbzjpn1#

我只是简单地加上

con.query(secorit, function (err, result) {
if (resultmohasebeh.length == 0) {
return;

它是一个数组,有时它是空的,因为它不存在于数据库中,并与array.length您可以注意到,如果一个数组是空的或没有,只需添加返回停止进程,它不会崩溃的应用程序,因为SQL

相关问题