Going nuts with this error

Going nuts with this error
0

#1

Hi all,

I need a little help trying to understand what am i doing wrong. I’m doing a web app for my programming class at the University and this error is not letting me advance.
The project consists in a web app that the user inserts some data and saves it into MySQL database.

The error that Node is showing is the following:

App listen in port 3000
Connected
req.body
{ primeiro: 'Ricardo',
  ultimo: 'Nunes',
  email: 'someemail@gmail.com',
  pais: 'Portugal',
  cidade: 'Almada',
  morada: 'Almada' }
ReferenceError: utilizadores is not defined

The table (utilizadores) exists but Express say’s otherwise. Here’s the index.js file:

//********************************
//EXPRESS INIT
//********************************
var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.static('public'));

app.listen(3000, function(){
    console.log('App listen in port 3000');
});

//********************************
//DATABASE INTEGRATION
//********************************
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    port: '3307',
    user: 'root',
    password: 'password',
    database: 'rco'
});

connection.connect(function(err){
    if(err) throw err
    console.log('Connected');
});

//********************************
//REGISTER INSERT
//********************************
app.get('/', function(req,res,next){
    res.sendfile('public/registo.html');
});

app.post('/myaction', function(req, res){
    console.log('req.body');
    console.log(req.body);
    res.write('You sent the first name "' + req.body.primeiro+'".\n');
    res.write('You sent the last name "' + req.body.ultimo+'".\n');
    res.write('You sent the email "' + req.body.email+'".\n');
    res.write('You sent the pais "' + req.body.pais+'".\n');
    res.write('You sent the cidade "' + req.body.cidade+'".\n');
    res.write('You sent the morada "' + req.body.morada+'".\n');
    //res.write('You sent the familia "' + req.body.familia+'".\n');
    res.end()

    connection.query("insert into "+utilizadores+" (PrimeiroNome, UltimoNome,Email, Pais,Cidade,Morada) values ('"+req.body.primeiro+"','"+req.body.ultimo+"','"+req.body.email+"','"+req.body.pais+"','"+req.body.cidade+"','"+req.body.morada+"')",function(err, result){
        if(err)
            throw err;
    });
});

Thanks in advance. This error is getting on my nerves…a simple select to the database retrieves the same error…


#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#3

Where is utilizadores defined?
Edit:
If it’s not then you can either define it in a var or just remove the “+ and +” between it.


#4

I don’t think the error is coming from the database, but from Node. Unless you want to define a constant somewhere

const UTILIZADORES = "utilizadores";

then you need to include the string literal with the rest of the string query

onnection.query("insert into utilizadores  (PrimeiroNome, UltimoNome,Email, Pais,Cidade,Morada) /* ... */ ")

#5

Sorry about the late reply guys. It worked, i’ve checked the insert code again, by the recommendation of PortableStick, and it worked.

I had the +utilizadores+ but forgot to set the variable utilizadores. Remove the plus signs and voila.

I can’t believe the time that i been with this error and it was a very simple mistake that i did…

Thanks again guys :slight_smile: