gpt4 book ai didi

mysql - 在 Node mysql 错误中处理安全

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:29 25 4
gpt4 key购买 nike

我的 app.js 中有一个方法可以处理 get 请求并进行 mysql 查询。当一切顺利时,这是一个圆满的结局,但如果出现任何问题,我会收到类似的消息。

/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Parser.js:78
throw err;
^

Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause'
at Query.Sequence._packetToError (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
at Query.ErrorPacket (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)
at Protocol._parsePacket (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Parser.js:73:12)
at Protocol.write (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/Connection.js:96:28)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
--------------------
at Protocol._enqueue (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Connection.query (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/mysql/lib/Connection.js:201:25)
at /Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/routes/routes.js:27:22
at Layer.handle [as handle_request] (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/layer.js:95:5)
at /Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:330:12)
at next (/Users/916784/Documents/Mobile Training/Node_Ex/SimpleAPI/node_modules/express/lib/router/index.js:271:10)

我当然知道为什么会出现这个错误,但我想安全地处理它。目前我的代码是

app.get('/deleteInventoryItem', function(req, res){
console.log('requested for deleting inventory item with id: ' + req.query.item_id);

if(req.query.item_id){
app.connection.query('UPDATE ITEMS.ITEMS_TABLE SET item_isdeleted=1 WHERE id=' + req.query.item_id,
function(err, rows){
if(err){
console.log('error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal);
next(err);
}
res.send({"status": "success",
"message": "Item deleted with id - " + req.query.item_id });
});
} else{
res.send({"status": "error", "message": "missing a parameter"});
}
});

下面是我写代码的地方

app.use(function(err, req, res, next) {
res.status(500).send({ error: 'Something failed!' });
});

如何处理错误部分,使我的 node.js 服务器不会崩溃?我如何发送适当的响应而不是发送整个错误堆栈跟踪

最佳答案

在普通的 NodeJS 中,您可以执行类似以下操作,而不是抛出错误:

app.connection.query('UPDATE ITEMS.ITEMS_TABLE SET item_isdeleted=1 WHERE id=' + req.query.item_id,
function(err, rows){
if(err){
console.log('error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal);

// Send error message with status as "failure" or smth explanatory
res.send({"status": "failure", "message": 'error occured in deleting: code - ' + err.code + " ,isFatal - " + err.fatal });
} else {
res.send({"status": "success", "message": "Item deleted with id - " + req.query.item_id });
}
});

关于mysql - 在 Node mysql 错误中处理安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34815445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com