我们可以使用ColdFusion的ArrayAppend()函数将DateTime附加到数组吗?

Tec*_*eam -1 sql sql-server coldfusion coldfusion-2016

错误:无法将对象类型[DateTime]强制转换为[Array]类型的值

<cfset Seniority=ArrayNew(1)>
  <CFLOOP QUERY="all_employees">
      <cfif isNull(all_employee.TimeInPositionDate) >
          <cfset ArrayAppend(Seniority,all_employee.hiredate)>
      <cfelse>
          <cfset ArrayAppend(Seniority,all_employee.TimeInPositionDate)>
      </cfif>
  </CFLOOP>
Run Code Online (Sandbox Code Playgroud)

Twi*_*len 6

您的问题源于CFML中的变量查找.当您在查询循环中时,ColdFusion将在变量范围之前从查询范围中提取.由于您的查询中有一列也称为Seniority您的代码读取相同ArrayAppend(all_employees.Seniority,all_employees.hiredate);

更改阵列的名称将解决手头的问题.

 <cfset all_employees = queryNew(
    "Id,TimeInPositionDate,Hiredate,Seniority",
    "Integer,Timestamp,Timestamp,Timestamp",
    [
        {"Id":1,"HireDate":Now(),"Seniority":Now()},
        {"Id":2,"HireDate":Now(),"TimeInPositionDate":Now(),"Seniority":Now()}
    ]
)>

<cfset arrSeniority=ArrayNew(1)>

<CFLOOP QUERY="all_employees">
    <cfif isNull(all_employees.TimeInPositionDate) >
        <cfset ArrayAppend(arrSeniority,all_employees.hiredate)>
    <cfelse>
        <cfset ArrayAppend(arrSeniority,all_employees.TimeInPositionDate)>
    </cfif>
</CFLOOP>
<cfdump var="#arrSeniority#"/>
Run Code Online (Sandbox Code Playgroud)