2021年3月27日星期六

Insert session data into postgres database in Nodejs Expess app

I have an Nodejs express function where I am trying to insert data that is stored in the browser session into my postgres database. When I have the insert statement like this, the insert works but the session-stored customer_id isn't inserted and is just left null.

On the line with "var sql = INSERT INTO journal....", the values $1 and $2 are from user input and work correctly.

How can I get value 3 of the customer_id stored in the session to insert correctly? I would appreciate any advice or greater understanding.

app.post("/addJournalEntry", addJournalEntry);  function addJournalEntry(req, res) {      console.log("Posting data");      // var id = req.query.id;      //body is for post, query is for get      const customer_id = req.session.customer_id;      const journal_entry_date = req.body.journal_entry_date;      const journal_entry = req.body.journal_entry;      const params = [journal_entry, journal_entry_date, customer_id];        addEntryFromDataLayer(params, function (error, addEntry) {          console.log("Back From the addEntryFromDataLayer:", addEntry);          if (error || addEntry == null) {              res.status(500).json({                  success: false,                  data: error              });          }           else {              // res.json(result);              res.status(200).json(addEntry);          }      });  }    function addEntryFromDataLayer(params, callback) {      console.log("addEntryFromDataLayer called with id");      var sql = "INSERT INTO journal (journal_entry, journal_entry_date, customer_id) VALUES($1::text, $2::text, $3)";      // var params = [id];      pool.query(sql, params, function (err, addEntry) {          if (err) {              console.log("error in database connection");              console.log(err);              callback(err, null);          }          console.log("Found DB result:" + JSON.stringify(addEntry));          callback(null, addEntry);      });  }  
https://stackoverflow.com/questions/66837801/insert-session-data-into-postgres-database-in-nodejs-expess-app March 28, 2021 at 10:05AM

没有评论:

发表评论