0

I'm building a program that queries MySQL databases, gets the tables, fields, field data types, and entries and returns it as a single object to be later used to view the MySQL data as a table.

This is what the built object will look like:

    {
    `Table_Name`: {
        Title: `Table_Name`,
        Fields: {
            `Field Name`: `Datatype`
        },
        RowData: []
    }

}

The query to get the tables is fine, however the query to get the row data isn't. The query function looks like this:

    function getRows(){

        let secondpromises = [];

        secondpromises.push(
            new Promise((resolve, reject) => {
                for(x in Tables){
                    Connect_SQL(SQLcreds, w_newSconn, (conn) => {
                        conn.query(`SELECT * FROM ${Tables[x]}`, (err, results) => {
                            if(err){
                                console.log(err);
                                reject(err);
                            }else{
                                for(r in results){

Tables[`${Tables[x].Title}`].RowData.push(results[r]);
                                }
                                resolve(results);
                            }
                        });
                    });

                    if(x == Tables.length - 1){
                        Promise.all(secondpromises).then(() => {
                            if(w_newSconn){
                                w_newSconn.close();
                                w_newSconn = null;
                            }
                            console.log(Tables);
                        });
                    }
                }
            })
        );
    }

The error is coming from conn.query(). It is throwing an error stating there is an error in my SQL syntax at:

SELECT * FROM [object Object]

I understand the reason why and I'm sure there is a way to resolve this through JSON.Stringify() but there must be a simpler way. I have already tried creating a variable like so:

let objArray = Object.keys(Tables)

But it still returned [object Object], any help would be appreciated.

4
  • i doubt this is possible.. MySQL normally does not allow preparing table and or column names in prepared statements.. Not sure if there is a translating layer in Node.js which makes this possible without SQL injection problems. Commented Oct 23, 2018 at 15:02
  • 1
    After Googling i found that it was possible in Node.js.. Possible duplicate of Nodejs-Mysql Query table name as a variable Commented Oct 23, 2018 at 15:06
  • Inserting a dynamic variable into a query isn't the issue. The problem I am running into is returning the name of the key instead of an object. I added an array that stores the keys and is now coming back with keys[n] being undefined. Commented Oct 23, 2018 at 15:26
  • The value of Tables[x] is an object, not a table name. The table name is presumably in a property, so that should be something like Tables[x].name (replace name with the actual name of the property that contains the table name). Commented Oct 23, 2018 at 15:44

2 Answers 2

0

Tables[x] is an object. You need to get the table name from it.

conn.query(`SELECT * FROM ${Tables[x].Title}`, (err, results) => {

It also looks like the property name is the same as the title, so you can do:

conn.query(`SELECT * FROM ${x}`, (err, results) => {
Sign up to request clarification or add additional context in comments.

Comments

0

I ended up creating a variable in the loop

let table = keys[x]

and that did the trick, for whatever reason ${keys[x]} was returning undefined but the variable returned the table name. Theoretically I could have changed the for loops to a

for(x in Tables)

and x would have returned the title so I may go back and rewrite it that way. Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.