Newbie question about returned data

52 views
Skip to first unread message

Mark Fuqua

unread,
Feb 13, 2017, 6:52:09 PM2/13/17
to Taffy Users
Disclaimer...newbie here, so forgive me if this is a stupid/ignorant question:

I have a test set up here:


This is what I am using for return varialbe:
return representationOf(qResult).withStatus(200);

It is returning records as so:
[1,"John","Fuqua"],

I thought it returned JSON as the default...

How do I get the data to return in this way instead:

  {
    "userID": 1,
    "FirstName": "John",
    "LastName": "Fuqua",
  },
  {
    "userID": 1,
    "FirstName": Mark,
    "LastName": "Fuqua",
  },
  {
    "userID": 1,
    "FirstName": "Bill",
    "LastName": "Jones",
  },


Thanks,

Mark

Adam Tuttle

unread,
Feb 13, 2017, 8:59:31 PM2/13/17
to Taffy Users
Hi Mark,

In my best clippy impression: It looks like you're returning a query object!

...which by default CF will serialize... oddly. For that reason Taffy ships with a helper to convert them to what most people are familiar with.

Try this:

return representationOf( queryToArray( qResult ) ).withStatus(200);

Here's the docs on queryToArray.

Also, just so you know, there's an alias "rep" for "representationOf", and 200 is the default return status, so if you're returning 200 then you don't have to specify it. So, you can get the same response and shorten the above statement to:

return rep( queryToArray( qResult ) );

Adam

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Fuqua

unread,
Feb 14, 2017, 8:20:17 AM2/14/17
to taffy...@googlegroups.com

Thank you Adam…I hope I’m not pushing the bounds of your good will, but…seems like I have a new error, you can see the error here:

 

http://www.mdcommercial.com/api/index.cfm/people

 

Here is the code for the component:

 

component
       
extends="taffy.core.resource"
       
taffy_uri="/people"
{
   
public function get()
    {

       
qGetPersonCollection = new Query(); // new query object
       
qGetPersonCollection.setSQL("SELECT UserID, FirstName, LastName FROM PlumUser"); //set query
       
qResult = qGetPersonCollection.execute(); // execute query

       
return representationOf(queryToArray(qResult)).withStatus(200);
    
}
}

 

It is pretty much copied and changed from the docs…I should probably mention, this is my first cfscript as well…though it was returning the result before, just in the ‘wrong’ format.  I simply added the queryToArray.

 

Thanks,

 

Mark


Adam

 

--

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to taffy-users...@googlegroups.com.

Adam Tuttle

unread,
Feb 14, 2017, 11:13:57 AM2/14/17
to Taffy Users
Hi Mark,

I would advise you not to use the query cfc object. If you're on a platform that supports it (CF11+), queryExecute() is a much better alternative. 

That said, I'll point a few things out to help you get better at debugging these things.

Firstly, you can and should configure IIS not to return its own error pages in the event of a non-200 status. This will be important if you want to return, for example, a 401 or 403 or 404 with a useful json body, because if you don't disable it then IIS will prepend its own content to your response body, as it's doing here, which will prevent the client from being able to parse the body: it's no longer valid json.

Once that's done you'll see just the JSON response. If we pluck out just the json response body that's there now, it does provide some useful information:

{
    "STACKTRACE": [
        {
            "RAW_TRACE": "\tat cfresource2ecfc344576604$funcQUERYTOARRAY.runFunction(C:\\inetpub\\wwwroot\\mdCommercial\\Taffy\\core\\resource.cfc:41)",
            "ID": "CF_UDFMETHOD",
            "TEMPLATE": "C:\\inetpub\\wwwroot\\mdCommercial\\Taffy\\core\\resource.cfc",
            "LINE": 41,
            "TYPE": "CFML",
            "COLUMN": 0
        },
        {
            "RAW_TRACE": "\tat cfpersonCollection2ecfc1098271373$funcGET.runFunction(C:\\inetpub\\wwwroot\\mdCommercial\\resources\\personCollection.cfc:13)",
            "ID": "CF_UDFMETHOD",
            "TEMPLATE": "C:\\inetpub\\wwwroot\\mdCommercial\\resources\\personCollection.cfc",
            "LINE": 13,
            "TYPE": "CFML",
            "COLUMN": 0
        },
        {
            "RAW_TRACE": "\tat cfapi2ecfc312111985$funcONREQUEST.runFunction(C:\\inetpub\\wwwroot\\mdCommercial\\Taffy\\core\\api.cfc:337)",
            "ID": "CFINVOKE",
            "TEMPLATE": "C:\\inetpub\\wwwroot\\mdCommercial\\Taffy\\core\\api.cfc",
            "LINE": 337,
            "TYPE": "CFML",
            "COLUMN": 0
        }
    ],
    "TAGCONTEXT": "C:\\inetpub\\wwwroot\\mdCommercial\\Taffy\\core\\resource.cfc [Line 41]",
    "DETAIL": "If the component name is specified as a type of this argument, it is possible that either a definition file for the component cannot be found or is not accessible.",
    "ERROR": "The Q argument passed to the queryToArray function is not of type query."
}

The error message is at the end and shows that you're not passing a query object to queryToArray. This is one small part of why I recommend queryExecute() over the Query cfc object. With the query object, you have to ask it for the resultset after you execute it. (Side note: if you're not on CF11+, I recommend iQuery instead. It wraps the Query cfc class but behaves very much like queryExecute().)

You can try changing your code to this:

qResult = qGetPersonCollection.execute().getResult();

Hope that helps,
Adam

On Tue, Feb 14, 2017 at 8:19 AM, Mark Fuqua <ma...@availdata.com> wrote:

Thank you Adam…I hope I’m not pushing the bounds of your good will, but…seems like I have a new error, you can see the error here:

 

http://www.mdcommercial.com/api/index.cfm/people

 

Here is the code for the component:

 

component
       
extends="taffy.core.resource"
       
taffy_uri="/people"
{
   
public function get()
    {

       
qGetPersonCollection = new Query(); // new query object
       
qGetPersonCollection.setSQL("SELECT UserID, FirstName, LastName FROM PlumUser"); //set query
       
qResult = qGetPersonCollection.execute(); // execute query

       
return representationOf(queryToArray(qResult)).withStatus(200);
    
}
}

 

It is pretty much copied and changed from the docs…I should probably mention, this is my first cfscript as well…though it was returning the result before, just in the ‘wrong’ format.  I simply added the queryToArray.

 

Thanks,

 

Mark


Adam

 

--

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.

To unsubscribe from this group and all its topics, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.

Mark Fuqua

unread,
Feb 14, 2017, 2:44:35 PM2/14/17
to taffy...@googlegroups.com

Adam,

 

qResult = qGetPersonCollection.execute().getResult();

Worked perfectly.

 

This is the first time (I can’t believe I’m admitting this ‘outloud’) I’ve used application.cfc, cfscript or Taffy. 

 

The framework I’ve been using since 2007 uses application.cfm…

 

Time to put on my big boy pants…thanks for the help.

 

Mark

 

From: taffy...@googlegroups.com [mailto:taffy...@googlegroups.com] On Behalf Of Adam Tuttle
Sent: Tuesday, February 14, 2017 11:13 AM
To: Taffy Users <taffy...@googlegroups.com>
Subject: Re: Newbie question about returned data

 

Hi Mark,

 

I would advise you not to use the query cfc object. If you're on a platform that supports it (CF11+), queryExecute() is a much better alternative. 


Hope that helps,

Adam

 


Adam

 

--

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--

You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.

To unsubscribe from this group and all its topics, send an email to taffy-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.

To unsubscribe from this group and all its topics, send an email to taffy-users...@googlegroups.com.

Adam Tuttle

unread,
Feb 14, 2017, 3:08:33 PM2/14/17
to Taffy Users
No worries man. Nothing to be ashamed of. No time like the present!

Adam


Hope that helps,

Adam

 


Adam

 

--

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--

You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.

To unsubscribe from this group and all its topics, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/joFJ0imdYaY/unsubscribe.

To unsubscribe from this group and all its topics, send an email to taffy-users+unsubscribe@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages