Modify the response
Updated: June 16, 2015
In a JavaScript backend mobile service, server scripts enable you to control what is written to the response. You can modify the results returned by an operation or even generate a different response.
The following script reads data from the database, appends an extra retrievedAt property to each result, and then writes those results to the response.
function read(query, user, request) { request.execute({ success: function(results) { var now = new Date(); results.forEach(function(item) { item.retrievedAt = now; }); request.respond(); //Writes the response } }); }
The following script bypasses calling the execute method when an existing record has the same value. It instead calls the respond method to report a success result. This avoids a duplicate record being inserted.
function insert(item, user, request) { var channelTable = tables.getTable('Channel'); channelTable.where({ uri: item.uri }).read({ success: insertChannelIfNotFound }); function insertChannelIfNotFound(existingChannels) { if (existingChannels.length > 0) { request.respond(statusCodes.OK, existingChannels[0]); } else { request.execute(); } } }
For more information, see the Mobile Services script reference
Show: