I am in the process of trying to copy a table from SQL server to MySQL using a NodeJS API. I need to function to run roughly every 30 minutes or so (I am using setInterval to do this).
I am encountering an issue when trying to write to the MySQL table, the column values are coming back null so nothing is being written and then an error is thrown. However, when I console.log that same data, I receive the entire table from SQL Server. I will attach the code below - there is only one file in the API so I will include it in its entirety.
import { createPool } from "mysql2";
import pkg from "mssql";
const { connect, query } = pkg;
const sql = pkg;
// MySQL Server
export const db = createPool({
host: "IP Address",
user: "root",
password: "Password",
database: "demo",
port: "3306",
connectionLimit: 300,
waitForConnections: true,
multipleStatements: true,
});
db.getConnection((err, connection) => {
if (err) throw err;
console.log("Database connected successfully");
connection.release();
});
// SQL Server
const config = {
user: "user",
password: "Password",
server: "Server Name",
database: "demo",
port: 1433,
options: {
trustedConnection: true,
encrypt: true,
enableArithAbort: true,
trustServerCertificate: true,
},
};
// Transfer function
const Transfer = () => {
sql.connect(config, function (err) {
if (err) console.log(err);
let sqlRequest = new sql.Request();
let sqlQuery = "Select * From access";
sqlRequest.query(sqlQuery, function (err, data) {
if (err) console.log(err);
if (data) {
db.query(
"INSERT IGNORE INTO attendance ID = ?, dateTime = ?, date = ?, time = ?, direction = ?, deviceName = ?, deviceSerial = ?, personName = ?, cardNumber = ?",
[
data.userID,
data.dateTime,
data.date,
data.time,
data.direction,
data.deviceName,
data.deviceSerial,
data.personName,
data.cardNumber,
],
(err) => {
if (err) {
console.log(err);
} else {
return;
}
}
);
}
});
});
};
setInterval(Transfer, 2000);
const port = 3001;
app.listen(port, () => console.log(`Listening on port ${port}`));
The table are exactly the same on both servers, even down to the datatype.
Please let me know if I can provide any other details. I would appreciate any help/guidance with this!