You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何验证MySQL表更新是否成功及访问affectedRows变量

Hey there! Let's walk through your questions about MySQL update operations in your Node.js code.

问题1:如何判断MySQL表的更新操作是否成功?

When you run an UPDATE query in MySQL via Node.js (like your verifyEmailAddressCode function does), the database returns an OkPacket object as the results parameter in your callback. The key indicator here is the affectedRows property:

  • If affectedRows > 0, it means the query found matching rows (that fit your email_address and verificationCode conditions) and successfully updated them.
  • If affectedRows = 0, no rows matched the criteria, so no updates were performed.

You can add a check right in your callback to confirm success, like this:

conn.query(updateSQL, function(err, results) {
  if(err){
    conn.release();
    return callback(err, null);
  }
  // Check if the update was successful
  const isUpdateSuccessful = results.affectedRows > 0;
  conn.release();
  // Pass back success status along with results
  return callback(null, { success: isUpdateSuccessful, data: results });
});
问题2:结果显示affectedRows: 0,如何访问该affectedRows变量?

That OkPacket object your query returns is exactly what's stored in the results parameter. You can access affectedRows directly using dot notation:

conn.query(updateSQL, function(err, results) {
  if(err){
    conn.release();
    return callback(err, null);
  }
  // Access affectedRows directly from the results object
  const rowsUpdated = results.affectedRows;
  console.log(`Number of rows updated: ${rowsUpdated}`); // Will log "0" in your case
  conn.release();
  return callback(null, results);
});

Since your current code already passes results to the callback, you can also access affectedRows wherever you call verifyEmailAddressCode:

verifyEmailAddressCode('user@example.com', '123456', function(err, results) {
  if (err) {
    // Handle error
    return;
  }
  console.log(results.affectedRows); // Logs 0
});

内容的提问来源于stack exchange,提问作者jdotdoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:21:25