Mark Joseph Barnette
Mark Joseph Barnette

Reputation: 51

MySQL query doesn't pull values in node.js

I am stumped, this is my first time working with both node and MySQL. I have managed to get both working. However my query doesn't seem to be using the values stored in my variables when it executes. It put in empty values everytime regardless of what the commands are. In this case I receive a string and split the array into commands. At most the array will have 3. I only use the first value in the query, in this test scenario it is A. Code follows:

console.log('Attempting to Create fireteam');
var ftcmds = suffix.split(" ", 3);
console.log(ftcmds);
            var Name = ftcmds[0];
            console.log('name is '+ Name);
            var ActivityDate = moment();
            console.log('the date is '+ActivityDate)
            var Owner = msg.author;
            console.log('the owner is '+ Owner);

            var ft = [Owner.id];
            console.log('the ft is '+ ft);
            var FireTeam = JSON.stringify(ft);
            console.log('the stringified object is '+ FireTeam);

            var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES (Name, Owner, FireTeam, ActivityDate)"
            //ftArr.push(ft);
            console.log("Attempting to run query "+ sql + "on conncetion" + mysqlcon);
            mysqlcon.query(sql, function (err, result) {
                        console.log("Result  |" +result);
                        console.log("Error   |" +err);
                        if (err) throw err;

                        console.log("1 record inserted");
                        msg.author.send("Created Fireteam ", name, ". I will setup a group chat on ", date, " if your team fills up. Go recruit guardians!");
                                
                        });

When I log the variables I can see they are updating with proper values, so I am stumped as to why the query would insert all empty values.

Upvotes: 0

Views: 64

Answers (1)

Rafael Oliveira
Rafael Oliveira

Reputation: 278

You should be concatenating those variables inside your query

so

    var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES (Name, Owner, FireTeam, ActivityDate)"

should actually be

    var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES ('" + Name + "','" + Owner + "','" + FireTeam + "','" + ActivityDate + "')"

Upvotes: 1

Related Questions