I'm new to Wix! Please forgive my ignorance. I'm trying to retrieve data from an external MySQL DB. I first approached the task by creating a back end "web module" on Wix using Node.js. I was able to install the node MySQL package and successfully connect to the external DB.
However, I've since been advised that this is not the smartest or safest way to go about doing this since credentials are exposed on the client side. Does anyone have suggestions for how this can be achieved?
I just want to make sure my efforts are being spent pursuing the correct approach. I've heard mention of creating an API on my DB and consuming on Wix via HTTP requests? This is new to me and I would have to research this but if it the preferred method then I will certainly look into.
Thanks
@tpolig01 - Thanks for posting your code, I've been attempting to do the same thing for a while now. Using your code as a guide I'm able to connect to my external DB make a static query, retrieve data and then pass the data to the front end, Perfect!
My issue is when I try and pass a variable from the front end to the back end to change the actual query I'm making to the DB. The query is made, the data is retrieved in the backend, but then everything stops and the data is not passed back to the front end. Any thoughts? I do not receive any errors or any thing that looks wrong, my code just stops. Have you had any experience like this?
All, I was able to figure it out!!!! Below is the code. Thanks to all for the help
Backend
export function getEmployeeNames() { return new Promise(function (resolve, reject) { var mysql = require('mysql'); var connection = mysql.createConnection({ host: 'xxxxxx', user: 'bob', password: 'xxx', database: 'xxx' }); connection.connect(function (err) { if (err) throw err; connection.query("SELECT Title FROM datasets", function (err, result) { if (result === undefined) { reject(new Error("Error result is undefined")); } else { resolve(result); } }) connection.end(); }); }); }
FrontEnd
export function button1_mouseIn(event) { getEmployeeNames() .then(function (results) { console.log(results); }) .catch(function (err) { console.log("Promise rejection error: " + err); }) //$w("#html1").postMessage(result); }
Check the article Sync your MySQL Database to Wix finally working (Article) from @Andreas Kviby.
Hi,
The right approach depends on what you are trying to achieve, what is the use case?
There is no reason for credentials to be exposed on the client side unless you expose them, check out this article about working with npm modules.