在不同情况下得到相同的错误

时间:2014-04-22 23:51:43

标签: coldfusion

请考虑以下代码:

我根据我在网址上指定的日期范围获取JSON数据。我将其反序列化为folows:

<cfhttp url="#urladdress#" method="GET" resolveurl="Yes" throwonerror="Yes">

 <cfset cfData=DeserializeJSON(CFHTTP.FileContent)> 

<cfset arraylen = ArrayLen(cfdata)>

我抓取数据并插入MySQL数据库,如下所示:

<cfquery datasource="XX.X.XX.XXX" name="qCoulmnInsert">
    INSERT INTO Mytable
     (aol_int,
     android_Phone_int,
     androidTablet_int,
     apple_mail_int,
     blackberry_int,
     Eudora_int,
     gMail_int,
     Hotmail_int,
     lotus_notes_int,
     other_int,
     other_webmail_int,
     Outlook_int,
     Postbox_int,
     sparrow_int,
     thunderbird_int,
     windowsLiveMail_int,
     yahoo_int,
     iPad_int,
     iphone_int,
     iPod_int,
    <!--- FOR DATES --->
     startdate_dt,
     enddate_dt,
     date_dt)

   VALUES
     <!--- loop through your array --->
     <cfloop from="1" to="#arrayLen(cfData)#" index="i">
     ( <!--- 1 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["aol"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 2 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Phone"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 3 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Tablet"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 4 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Apple Mail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 5 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Blackberry"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 6 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 7 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["gmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 8 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["hotmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 9 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
     <cfelse>
     NULL
     </cfif>, 

     <!--- 10 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 11 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other Webmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 12 ---> 
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Outlook"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 13 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Postbox"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 14 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Thunderbird"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 15 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Sparrow"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 16 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Windows Live Mail"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 17 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Yahoo"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 18 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPad"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 19 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPhone"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 20 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPod"]#">
     <cfelse>
     NULL
     </cfif> ,

  <!--- FOR DATES --->
     <cfif structKeyExists(cfData[i], "startdate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].startdate_dt#">
     <cfelse>
     NULL
     </cfif>, 

     <cfif structKeyExists(cfData[i], "enddate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].enddate_dt#">
     <cfelse>
     NULL
     </cfif>,

     <cfif structKeyExists(cfData[i], "date")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].date#">
     <cfelse>
     NULL
     </cfif> 

    )

     <cfif i neq arrayLen(cfData)>,</cfif>
     </cfloop>
    </cfquery>

我面临的问题:

当JSON数据没有&#34; Lotus Notes&#34;在其中,我得到一个错误&#34;元素Lotus Notes在作为表达式的一部分引用的CFML结构中未定义。 &#34;

指向<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">

当它在JSON中有Lotus Notes时,它会获取一些其他元素并且我得到了错误&#34; Element Eudora在作为表达式的一部分引用的CFML结构中未定义。 &#34; 并指向上面代码中的<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#">

Here是日期为01月1日的Lotus Notes示例JSON

here它没有Lotus Notes(从1月02日开始)字段,我得到Eudora错误

我不明白我错在哪里。请指教

2 个答案:

答案 0 :(得分:1)

您只需要检查cfData[i].Open是否存在 - 如果不是所有这些整数都为空,那么只输出20个空值。 (或者根本不打扰插入 - 取决于这里发生了什么)。

此外,您不需要为每一个都使用显式硬编码逻辑 - 只需使用一个循环并使用一段代码检查循环内部是否存在 - 即:

<cfif StructKeyExists(cfData[i],'open')>

    <cfloop index="Key" array=#['aol','Android Phone','Android Tablet','etc']# >

        ,
        <cfif StructKeyExists( cfData[i].open , Key ) >
            <cfqueryparam value=#cfData[i].open[Key]# cfsqltype="cf_sql_integer" />
        <cfelse>
            <cfqueryparam value="" null=true />
        </cfif>

    </cfloop>

<cfelse>

    #RepeatString( ',null' , 20 )#

</cfif>

答案 1 :(得分:0)

我不明白为什么你在参考之前没有检查。这就是我写它的方式。

 <!--- 1 --->
 <cfset data = cfData[i]>
 <cfset isVarDefined = isDefined("variables.data.open.aol")>
 <cfset value = isVarDefined ? data.open.aol : "">
 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#value#" null="#!isVarDefined#">

 <!--- 2 --->
 <!--- very similar to the code above... --->