App.post returning empty set of curly braces

Viewed 340

I'm building an express.js application that just takes data from A mysql database and displays on the screen and I'm trying too implement an insert functionality so I can add too the database via browser, when I post the results too one of the route points I am returned empty braces and do not know why,any help at all would be appreciated.

Index.js below this is addCountries.ejs

//brings you too add Country
app.get("/addCountries", (req, res) => {
  res.render("addCountries")
  console.log("hello")
})

//inserts data from add countries
app.post("/addCountries", (req, res) => {
  sqlDAO.addCountry()
    .then((data) => {
      res.render("addCountries", {
        addCountries: data
      })
      console.log(req.body.co_name)
      console.log("hello")
    })
    .catch((error) => {
      res.send(error)
      console.log("hello")

    })
})
<h1>Add Country</h1>
<br>
<br>
<form action="/addCountries" method="POST">
  <label for="cCode">Country Code:</label>
  <input type="text" id="cCode" name="co_code"><br><br>
  <label for="cName">Country Name:</label>
  <input type="text" id="cName" name="co_name"><br><br>
  <label for="CDetails">Country Details:</label>
  <textarea type="text" id="CDetails" name="co_details"></textarea>
  <input type="submit" value="Add">
</form>

SQLDAO.js

var pool



//creates pool based on database provided by project spec
mysql.createPool({
    connectionLimit: 3,
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'geography'
  })
  .then((result) => {
    pool = result
  })
  .catch((error) => {
    console.log(error)
  })

var addCountry = function() {
  // returns new promise
  return new Promise((resolve, reject) => {
    // function that adds too database
    var myQuery = {
      sql: "INSERT INTO country VALUES (?, ?, ?)",
      values: [req.body.co_code, req.body.co_name, req.body.co_details]
    }

    pool.query(myQuery)
      .then((data) => {
        resolve(data)
        console.log(data)
      })
      .catch(error => {
        reject(error)
      })

  })
}

1 Answers

You will need to pass a reference to the request object to addCountry(). This is because the addCountry() function inside the SQLDAO.js file does not have access to the request object.

Right now, in addCountry() the req variable is undefined, so when the SQL statement is compiled there is no data to insert. If you look in the DB you'll likely see empty or no records being added.

By passing in the request object, it has data to place into the db and that the db can return.

Edit both files like this:

sqlDAO.addCountry(req)... then var addCountry = function(req) {...

There are two reasons this is necessary:

  1. Inside your app.post() function both req and res are scoped locally to the function and are unavailable outside that function.

  2. Even if they were pseudo-global, variables are only available within the module they are created within. So you must export that variable or, in this case, pass a reference to it to some other function within some other module.

Related