存取Node.js 中的RowDataPacket 物件
查詢本地MySQL 資料庫時,可能會遇到需要存取結果的情況,儲存在名為“rows”的數組中。這些結果是 RowDataPacket 對象,它允許您從查詢中檢索值和鍵。
要存取這些值,您可以簡單地使用物件的屬性名稱,如控制台輸出中所示:
console.log(row.user_id); // Output: 101
RowDataPacket 物件本身就是建立這些物件的建構函數,因此您可以使用constructor.name 存取它property:
console.log(row.constructor.name); // Output: RowDataPacket
如果結果陣列包含多個RowDataPacket對象,您將使用 row[0] 存取第一個物件的屬性:
console.log(rows[0].user_id); // Output: 101
這種使用物件屬性的方法給出您可以存取 RowDataPacket 物件的值和鍵。
以上是如何從 Node.js MySQL 查詢中的 RowDataPacket 物件存取資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!