Testing out the database by sending values from an HTML form has been successful so far. Unique inserts and good connections are working well. However, intentionally sending a duplicate username for testing purposes results in an error message - which is exactly what I want!
The challenge now is to extract the cause of the error, which happens to be the key. The error object looks like this when checked with typeof(error):
Error: Insert into Users
(password
, role
, username
) values ('1112131234434', 'basic', 'Cthulu') - Duplicate entry 'Cthulu' for key 'username'
- Attempting to extract the key after converting the object to JSON using JSON.stringify only gives part of the message: {"code":"ER_DUP_ENTRY","errno":1062,"sqlState":"#23000"} Missing is the other details needed. Is it not possible to convert the entire error to a string then perform a substring search for the key? Or am I approaching this problem incorrectly?
Additionally, trying to traverse through the keys of the object reveals that while it seems to be a string, it actually isn't. Extracting the specific part of the string as shown in "'key 'username'" from the error remains a challenge.
In essence, my aim is to send back the key 'username' to the client form indicating that their chosen username is already taken. The same process will later apply to the email, hence my need to identify and extract the key.
Edit: Considering implementing stored procedures with GET DIAGNOSTICS seems to be the next step. Following guidelines similar to this article might provide a solution.