Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

Weird If/Else Problem with Javascript & Express

I'm having a problem checking if a object is undefined or not. Weirdly, this error doesn't occur in Mac OSX but on Windows I get a "cannot read property of to_char" error. Hoping you folks can help me!

Here is the code:

let accountInfo = {};
let recentForms = {}

  db.tx(async (t) => {
    accountInfo = await t.one(
      "SELECT * from useraccount INNER JOIN company on useraccount.company_id = company.id WHERE username = $1",
      [req.params.username]
    );
    recentForms = await t.any(
      "SELECT f.form_id, TO_CHAR(f.date_submitted :: DATE,'yyyy-mm-dd'), f2.form_name from formresponse f INNER JOIN forms f2 on f.form_id = f2.form_id WHERE f.company_id = $1",
      [accountInfo.company_id]
    );
    return accountInfo, recentForms;
  })
    .then((data) => {
      // We need to compare the form submitted date to our date today to determine if completed.
      // First we need to see if to_char exists and then decalre the forms date as a date variable
      if (recentForms[0].to_char !== undefined){
        formDate = new Date(recentForms[0].to_char);
      }
      else{
        formDate = '2120-01-12' 
      }

Basically, if pg-promise returns something for recentForms[0].to_char, i want to turn it into a javascript date variable. If there's nothing, I want to set formDate as a far in the future variable. I can't get the program to execute that if statement though, atleast on Windows.

Appreciate any help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...