Windows Azure移动服务:不要多次插入相同的数据

时间:2013-08-14 20:37:33

标签: javascript windows-phone-7 azure windows-phone-8 azure-mobile-services

我在Windows手机中有一个应用程序,并在Facebook上进行注册,我将一些数据存储在一个表中,但是,单个用户在表中存储了多次。我尝试(获取fb-id)检查表是否具有该fb-id的记录,但在重新注册之前新用户应该检查是否存在,例如异步方法没有顺序并始终首先执行查询插入,因为我可以解决这个问题?

客户端(有限)

                    await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
                    Message = string.Format("User Authenticate - {0}", App.MobileService.CurrentUser.UserId);

                    //***** Get fb info
                    var userId = App.MobileService.CurrentUser.UserId;
                    var facebookId = userId.Substring(userId.IndexOf(':') + 1);
                    var client = new HttpClient();
                    var fbUser = await client.GetAsync("https://graph.facebook.com/" + facebookId);
                    var response = await fbUser.Content.ReadAsStringAsync();
                    var jo = JObject.Parse(response);
                    var FbidUser = jo.GetValue("id");
                    var userName = jo.GetValue("name");
                    var genero = jo.GetValue("gender");

但是,我应该在服务器客户端做,但是如何插入信息数据一次,我的意思是,在插入之前检查表中的记录。 服务器端Azure:

function insert(item, user, request) 
{
  item.UserName = "<unknown>"; // default
  var identities = user.getIdentities();
  var req = require('request');

  if (identities.facebook)
  {
    var fbAccessToken = identities.facebook.accessToken;
    var url = 'https://graph.facebook.com/me?access_token=' + fbAccessToken;

    req(url, function (err, resp, body)
    {
        if (err || resp.statusCode !== 200)
        {
            console.error('Error sending data to FB Graph API: ', err);
            request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
        }
        else 
        {
            try 
            {
                var userData = JSON.parse(body);
                item.UserName = userData.name;
                request.execute();
            } catch (ex) 
            {
                console.error('Error parsing response from FB Graph API: ', ex);
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
            }
        }
    });

  }
}

根据上面的代码,我有第二个问题,在facebook的开发者中心,我可以访问电子邮件和照片(我的应用程序),我猜 userData 变量包含这个信息?我如何访问它?,如何调用其他信息的那些属性?

1 个答案:

答案 0 :(得分:2)

如果您想要的是阻止插入具有相同名称的用户的两个项目,最简单的方法是根本不使用该名称,而是使用FB ID(毕竟,这可能是两个不同的人们有相同的名字)。您可以使用以下脚本:

function insert(item, user, request) 
{
    item.UserId = user.userId;
    var currentTable = tables.current;
    currentTable.where({ UserId: user.userId }).read({
        success: function(results) {
            if (results.length > 0) {
                // an item with that user id already exists in the table
                request.respond(400, 
                    { error: 'item already in the table' });
            } else {
                // new user, can insert it here
                request.execute();
            }
        }
    });
}

现在,如果您真的想要将用户名称用作表格的“关键字”,那么您也可以做类似的事情:

function insert(item, user, request) 
{
  item.UserName = "<unknown>"; // default
  var identities = user.getIdentities();
  var req = require('request');
  var currentTable = tables.current;

  if (identities.facebook)
  {
    var fbAccessToken = identities.facebook.accessToken;
    var url = 'https://graph.facebook.com/me?access_token=' + fbAccessToken;

    req(url, function (err, resp, body)
    {
        if (err || resp.statusCode !== 200)
        {
            console.error('Error sending data to FB Graph API: ', err);
            request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
        } else {
            try {
                var userData = JSON.parse(body);
                item.UserName = userData.name;
                currentTable.where({ UserName: item.UserName }).read({
                    success: function(results) {
                        if (results.length > 0) {
                            request.respond(statusCodes.BAD_REQUEST,
                                { error: 'Name already in the table' });
                        } else {
                            request.execute();
                        }
                    }
                });
            } catch (ex) {
                console.error('Error parsing response from FB Graph API: ', ex);
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
            }
        }
    });

  }
}