Promise rejection error when trying to INSERT array to mysql table.

I’m querying a data collection and attempting to push the results to a mysql table via npm-mysql. When I try to INSERT the data into the table, I get a promise rejection error. What is strange is that I can successfully insert into the table if I only use one variable for all fields. I don’t understand this issue.


I am querying a data collection and then attempting to push the results to the getData2 function.

$w.onReady( function () {

$w("#button1").onClick((event) => { 

    wixData.query("fran_collection") 
        .find() 
        .then((results) => { 

            getData2(results) 
                .then( **function**  () { 
                }) 
                . **catch(function**  (err) { 
                    console.log("Promise rejection error: " + err); 
                }) 
        }); 
}); 

});


export function getData2(results) {
return new Promise( function (resolve, reject) {
var mysql = require (‘mysql’);
var connection = mysql.createConnection({
host: ‘’,
user: ‘’,
password: ‘’,
database: ‘’
});
connection.connect( function (err) {

for ( let x = 0; x < results.length; x++) {

let Email = results.items.title
let Org = results.items.newField
let Affil = results.items.affil

let sql = INSERT INTO members (Email,Org, Affil) VALUES ('${Email}','${Org}','${Affil}');

            connection.query(sql,  **function**  (err, result2) { 

if (result2 === undefined) {
reject( new Error(“Error result is undefined”));

                }  **else**  { 
                    resolve(result2) 
                } 

            }); 
        } 
        connection.end(); 

    }); 
}); 

}

What is strange is if I make the sql statement

let sql = INSERT INTO members (Email,Org, Affil) VALUES ('${Email}','${Email}','${Email}');

with every field value set to Email it works…but as soon as I try to change a field to a different variable I get the promise rejection error. Any ideas? This one has me stumped.