首页 > 解决方案 > Javascript 和 Express 出现奇怪的 If/Else 问题

问题描述

我在检查对象是否未定义时遇到问题。奇怪的是,这个错误在 Mac OSX 中不会发生,但在 Windows 上我得到一个“无法读取 to_char 的属性”错误。希望各位大侠能帮帮我!

这是代码:

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' 
      }

基本上,如果pg-promise 为recentForms[0].to_char 返回一些东西,我想把它变成一个javascript 日期变量。如果什么都没有,我想将 formDate 设置为远在未来的变量。不过,至少在 Windows 上,我无法让程序执行该 if 语句。

感谢任何帮助!

标签: javascriptexpresspg-promise

解决方案


推荐阅读