我试图添加一个帖子请求到一个网站,但我不明白我正在收到的错误。我发送了四个文本元素,我确信它们是正确的(大小、类型),并且我设法在我的数据库中添加了一个元素,没有问题。这不是我的网站,我真的不知道它是如何工作的,所以我复制了另一个请求。然而,错误是说我的元素之一太长,这不是事实,所以我有点困惑。
这是我的邮件请求
app.post("/api/addOS" , function(req, res) {
if(!(apikeys[req.query.username]===req.query.apikey) || (req.query.username == undefined) || (req.query.apikey == undefined)) {
res.json({"error" : "not allowed"});
} else {
var con = new Database();
var query = "INSERT INTO BRAND (name,abbr,color,type) VALUES ('"+req.body.name+"','"+req.body.abbr+"','"+req.body.couleur+"','"+req.body.type+"')";
con.query(query).then(rows => {
res.json(rows);
});
}
});类数据库的定义如下
class Database {
constructor( ) {
this.connection = mysql.createConnection( {
host: "localhost",
user: "root",
password: "pswd",
database: "dbname"
} );
}
query( sql, args ) {
return new Promise( ( resolve, reject ) => {
this.connection.query( sql, args, ( err, rows ) => {
if ( err ){
return reject( err );
}
resolve( rows );
} );
} );
}
close() {
return new Promise( ( resolve, reject ) => {
this.connection.end( err => {
if ( err )
return reject( err );
resolve();
} );
} );
}}
显示在我的网页控制台上的错误是这个
angular.js:14525可能无法处理拒绝:{“数据”:null,“状态”:-1,“config”:{“方法”:“POST”,"transformRequest":null,"transformResponse":null,“jsonpCallbackParam”:“回调”,“url”:“https://localhost:4443/api/addOS?username=test&apikey=z4oP>3 3Jocv”,"headers":{"Accept":"application/json,text/平原,/"},"name":"testtest","abbr":"test","type":"os","couleur":"tre"},“statusText”:“}
我控制台上的那个是这个
(C:\wamp64\www\node\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14) (节点:15728) UnhandledPromiseRejectionWarning: ER_DATA_TOO_LONG:数据过长,列'abbr‘位于Query.Sequence._packetToError第1行,at Query.ErrorPacket UnhandledPromiseRejectionWarning at Protocol._parsePacket (C:\ )wamp64\www\node\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (C:\wamp64\www\node\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (C:\wamp64\www\node\node_modules\mysql\lib\protocol\Parser.js:43:10) at Protocol.write (C:\wamp64\www\node\node_modules\mysql\lib\ )( protocol\Protocol.js:38:16)在插座处。(C:\wamp64\www\node\node_modules\mysql\lib\Connection.js:91:28)在套接字处。(C:\wamp64\www\node\node_modules\mysql\lib\Connection.js:525:10) at Socket.emit (events.js:223:5) at addChunk (_stream_readable.js:309:12)在Connection.query Connection.query at C:\wamp64\www\node\app.js:92:29 at new Promise () at Database.query (C:\wamp64\www\node\app.js:91:16) at C:\wamp64\www\node\app.js:379:9 at Layer.handle as handle_request at next(C:\wamp64\www\node\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\wamp64\www\node\node_modules\express\lib\router\route.js:112:3) at Layer.handle as handle_request (节点:15728) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。(拒绝id: 1) (节点:15728) DEP0018 DeprecationWarning:未处理的承诺拒绝被取消。在未来,承诺不处理的拒绝将使用非零退出代码终止Node.js进程.。
发布于 2020-03-16 04:03:10
我看到了共同的层面问题:
1)不要在请求时创建与数据库的连接。
2)您的数据库类允许使用带有args替换的承诺(在我的示例中是?符号),所以使用它,它更安全。
您说过abbr字段是varchar(8),所以在我的示例中,req.body.abbr.trim()必须清除可能是问题所在的空符号。
请试试这段代码并告诉我结果。
const db = new Database(); // Connection must be created once
// authorization
const isAuthorized = function(req, res, next) {
if(
req.query.username &&
req.query.apikey &&
apikeys[req.query.username] === req.query.apikey
) {
return next();
}
res.status(401).json({"error" : "not authorized"});
};
app.post(
"/api/addOS",
isAuthorized,
async function(req, res) {
try {
const result = await db.query(
'INSERT INTO BRAND (name, abbr, color, type) VALUES (?, ?, ?, ?)',
[req.body.name, req.body.abbr.trim(), req.body.color, req.body.type]
);
res.status(201).json(result);
}
catch (error) {
res.status(500).json({message: error.message});
}
});https://stackoverflow.com/questions/60700017
复制相似问题