簡體   English   中英

如何處理 MySQL 節點查詢?

[英]How to handle a MySQL node query?

在 Node JS 中運行 MySQL 查詢時,我正在嘗試處理 dup key 錯誤。 我嘗試將呼叫包裝在 try-catch 中,但仍然發生崩潰。 我還將創建調用包裝在 try-catch 中,但沒有運氣。 有誰可以處理這個崩潰?

create(first_name, last_name, username, email, password, callback) { 
    let salt = bcrypt.genSaltSync(SALT_ROUNDS);
    let hash = bcrypt.hashSync(password, salt);

    let sql = `INSERT INTO users (username, email, first_name, last_name, password) VALUES (?, ?, ?, ?, ?);`;
    let values = [
        username.toLowerCase(), email.toLowerCase(), 
        first_name.toLowerCase(), last_name.toLowerCase(), 
        hash
    ];

    // Crash below 
    try {
        db.query(sql, values, (error, results) => {
            if (error) {
                return callback(error, undefined);   
            }
            return callback(undefined, results.insertId)
        });
    } catch (error) {
        callback(error, undefined)
    }
}

NodeJs 路由調用:

Users.create(first_name, last_name, username, email, password, (error, insertedId) => {
    if (error) {
        next(error);
    }
    ..
}

錯誤:

Database connected...
Error: ER_DUP_ENTRY: Duplicate entry 'test-test@gmail.com' for key 'username'
at Query.Sequence._packetToError (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\main\development\hvz-api\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\Users\main\development\hvz-api\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:223:5)
at addChunk (_stream_readable.js:309:12)
..
..

C:\Users\main\development\hvz-api\node_modules\mysql\lib\protocol\Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:485:11)
    at ServerResponse.header (C:\Users\main\development\hvz-api\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\main\development\hvz-api\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\main\development\hvz-api\node_modules\express\lib\response.js:267:15)

由於您已經在代碼的相關部分周圍包裹了一個 try-catch 並且仍然存在崩潰,因此可以安全地假設崩潰發生在catch內部,即從try塊拋出異常, catch塊捕獲它然后在你的catch塊內發生了一些事情,這引發了另一個異常。 您可以通過暫時注釋掉catch中的代碼來嘗試我的理論,只是為了好玩:

try {
    db.query(sql, values, (error, results) => {
        if (error) {
            return callback(error, undefined);   
        }
        return callback(undefined, results.insertId)
    });
} catch (error) {
    //callback(error, undefined)
}

如果它現在沒有拋出異常,那么您對回調的調用會引發錯誤,您將需要更改catch中的代碼,以便它正常失敗。

但這只是技術解決方案。 從概念上講,您甚至不應該運行可能無效的insert 您應該執行驗證檢查,如果一切正常,則運行insert 您仍然需要圍繞它的 try-catch 和適當的錯誤處理來處理驗證檢查和insert之間發生的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM