Skip to content
Advertisement

SQL transaction with JS Array of Objects?

Say i have an Array of Objects

Now i create a comma seperated list

To pass it to sql.transaction?

is there any other way then to loop through the array and pass object each by one or can i pass bulk array? When i pass the comma_seperated_list_response it says Error object Object

Advertisement

Answer

If every object into your array should be one row into the table, then what you are trying to do is SQL Insert Multiple Rows. That is, we are inserting multiple objects using only one insert command. So, each object may have many attributes, not just name, but also age, for example.

So, for every object, you have to create the SQL tuple with the values (name, age).

Finally, in the SQL script, they must be grouped in a comma-separated list of values in a command like this:

One possible way to do it is like the code below:

But, this is still fragile to SQL Injection. So, let’s try something similar to these suggestions from What are Best Practices for preventing SQL injection in node-mysql?:

This command should create the bind SQL command and the values pointing to each value:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement