SPRZEDAŻ: 1-800-867-1389
EN
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

Modify the response

Updated: April 2, 2014

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

Czy oceniasz te materiały jako pomocne?
(Pozostało znaków: 1500)
Dziękujemy za opinię.
Pokaż:
© 2014 Microsoft