在HighScore表中插入或更新记录

时间:2013-05-05 20:42:39

标签: azure-mobile-services

我有一张收到高分条目的表格。但是,如果用户已经在表中有一个条目(通过GUID字段跟踪,而不是用户参数),我想更新它,如果新条目有更好的时间,否则不要更改现有记录。但是,如果用户在高分表中没有记录,则添加新记录。我还有两个查询参数传递给查询。

我希望insert操作能够为表处理这个问题。到目前为止我有这个,但是当我在高分榜上调用InsertAsync(...)时,我得到一个异常

function insert(item, user, request) {
    var sql ="select Id from HighScore where PlayerGUID=? AND PlayerBadge=?";
    mssql.query(sql, [user.PlayerGUID], [user.PlayerBadge], {
        success: function(results) {
            if(results.length > 0) {
                // leader board record exists so update the current record
                // Check the existing record and update it is the new time is better
                console.log("Found existing entry");
            } else {
                // no record exists for this user to insert one
                request.execute();
                console.log("Found existing entry");
            }
        }
    });
}

有人可以帮我实现目标吗?

非常感谢,

学家

1 个答案:

答案 0 :(得分:1)

花了一些时间和一些帮助,但这里是我最终的地方。它就像我预期的那样工作。


function insert(item, user, request) {
    // Store the passed in item object for us when inserting or updating
    resultsItem = item;
    // Store the request object to allow calld functions to send respond commands
    thisRequest = request;
    // Retrieve the HighScore table so we can check it for an existing record
    hsTable = tables.getTable('HighScore');
    // Update the leaderboard
    updateLeaderboard(item);
}

// Global variables
var resultsItem, hsTable, thisRequest;

function updateLeaderboard(item){
    //Filter the table using the where operator to only include those
    // records for the current PlayerGUID and PlayerBadge fields
    hsTable.where({
        PlayerGUID: item.PlayerGUID,
        PlayerBadge: item.PlayerBadge
        }).read({
        success:updateScore,
        error: errorHandler
        })
}

function updateScore(results){
    if(results.length > 0) {
    // If a record already exists then check the PlayerTime
        if(results[0].PlayerTime > resultsItem.PlayerTime)
        {
            // Update the PlayerTime if it is less than the currently saved value
            hsTable.update({
                id: results[0].id,
                PlayerTime: resultsItem.PlayerTime
            }, {
                success: logSuccess,
                error: errorHandler
            })
        } else {
            // Send them OK. Could change this and use the returned code/text to display a custom
            // message that tells the user that a previous time is faster.
            thisRequest.respond(statusCodes.OK);
        }

    } else {
        // The record for this PlayerGUID and PlayerBadge exists so write one
        hsTable.insert({
            PlayerName: resultsItem.PlayerName,
            PlayerCountry: resultsItem.PlayerCountry,
            PlayerTime: resultsItem.PlayerTime,
            PlayerBadge: resultsItem.PlayerBadge,
            PlayerGender: resultsItem.PlayerGender,
            PlayerDOB: resultsItem.PlayerDOB,
            PlayerGUID: resultsItem.PlayerGUID
        }, {
            success: logSuccess,
            error: errorHandler
        })
    }
}

// Called if there is an error
function errorHandler(error){
    console.error
    ("An error occurred trying to update leaderboard infor for player" +
    resultsItem.PlayerName);
    thisRequest.respond(statusCodes.BAD_REQUEST);
}

//Called if things work out ok.
function logSuccess()
{
    thisRequest.respond(statusCodes.OK);
}