Category: How do I do X?


This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Simple formatting for MySQL LAST_INSERT_ID() call?


I am struggling with retrieving data from MySQL queries in my JavaScript code. In one case, I need to retrieve the auto-incremented primary key from the database query, but the returned value has parentheses that prevent me from accessing the ID directly. In another case, I am getting a JSON object with one key-value pair, but I only need the value of the first key. I received helpful solutions from Amelia, such as using square brackets to access the key with parentheses, or using Object.keys() to access the value of the first key in the JSON object.


The issue was with retrieving the auto-incremented primary key value from the result of a MySQL query in a JavaScript object. The usual approach of lastInsertID[0].LAST_INSERT_ID() did not work due to residual parentheses from the SQL command. The solution was to use lastInsertID[0]['LAST_INSERT_ID()'].

In another case, the result of an EXISTS query returned a JSON object with dynamically changing parameters. To retrieve the value of the first key of the first JSON object returned, the solution was to use[0][Object.keys([0])[0]].

Both solutions were found through helpful advice and collaboration with Amelia.