event.renderData is always empty

I’m creating a RESTful API starting with the RESTful template. Can someone tell me why when I use event.renderData always returns empty data?

Here my simple handler:
`

function years( event, rc, prc ) {

// This is the format we want!

var test = [

{name=“Tricia”, lastname=“Smith”, nickname=“Trish”},

{name=“Jack”, lastname=“Doe”, nickname=“JJ”}

];

//prc.response.setData(test); // this works!

event.renderData( type=“json”, data=test ); // this never works.

}

`

event.renderData always returns this below regardless if I use a Query variable, struct or database Query.

{
errorcode: 0,
error: false,
data: “”,
messages: [ ]
}

Because the rest template leverages the response object used in the base handler. Basically there is no need to leverage render data explicitly.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057

Hi Luis,

I’m back to this same issue. I want to render data back as JSON, XML, TEXT, HTML, etc. (see Rendering Data with Formats: http://wiki.coldbox.org/wiki/EventHandlers.cfm)

How can my REST api return back a data download file?

Thanks,

Jeff

You mean, deliver a file?

Luis F. Majano
CEO
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Social: twitter.com/lmajano facebook.com/lmajano

Yes, return a file from the REST handler. I’d like to return a query result in a csv formatted file as a download. I think they’ll want the json as well but not wrapped in the typical response and just the data section. However I have tried to implement per the documentation and it only returns empty JSON.

Client wants this change pretty quick; really appreciate the help.

Thanks,
Jeff

Jeff,

You’ll need to send custom headers for that download anyway. Easiest do in a one-off handler method that delivers the spreadsheet and add a conditional in the current action to deliver the file if the flag is passed. There’s a downloadFile method in this “QueryToCSV” helper, should get you started (though it performs an immediate flush/abort ):

https://gist.github.com/jclausen/8429aba7672185421e743554657b34dd

Jon

Thanks Jon, very appreciated. I think I can get started with the QueryToCSV, thanks for that. As for the handler, how would I call downloadFile(filePath)? How far off is this example where my Route points to this function below? I’m not sure what to return. Also, I don’t think QueryToCSV takes the Query object right?

function DataDownloadHandler(event, rc, prc) {
queryService = new Query();
queryService.setDatasource(“myDb”);
queryService.setSQL(myQueryString)"

fileToDownload = QueryToCSV(queryService,“DataFile”,true);

// Seems more needs to happen here…
downloadFile(fileToDownload);
}