Skip to content
Advertisement

Display data after user input form (mysql-nodejs)

I have a page where there is a form, in which user will fill inputs. Then, I redirect to another page in which depending on the user’s choices some data will be displayed (the data will come from a mysql database). This is my code:
index.js (here are my routes)

JavaScript


airTicketsController.js (a controller where mysql queries will be executed)

JavaScript


air_tickets.ejs (the page for the form)

JavaScript


air_ticketsSelect.ejs (the page for displaying the data)

JavaScript


In general, I think this is supposed to work. However, it doesn’t display the data, only an empty table. If I do the query manually (e.g. SELECT * FROM flight WHERE from_destination = ‘Paris’ AND to_destination = ‘London’), it will work correctly..
When I console.log the query it says that:
sql: ‘SELECT * FROM flight WHERE from_destination=NULL AND to_destination=NULL’, values: [ undefined, undefined ]

Advertisement

Answer

You are mistakenly double defining variables.

In exports.displayFlights there is no need to again initialize the variables and removing those two lines will solve your problem.

Since, there is no req.body in exports.displayFlights your variables are getting initialized to undefined.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement