Azure移动服务javascript从插入功能更新

时间:2013-03-26 00:09:34

标签: javascript mobile azure

我正在为windows phone 8创建一个移动应用程序,iOs android。我正在使用windows azure来保存一些配置文件应用程序和一些设备信息。我对JavaScript的经验很少,虽然我整天撞到砖墙后开始点击我的想法。据说你可能会嘲笑我下面的代码。

这(下面)是名为Devices的表的插入语句。

如果当前没有userId的记录,我试图进行正常插入。

如果已有记录,则改为更新该记录。

function insert(item, user, request) {
  item.userId = user.userId;

  var deviceTable = tables.getTable('Devices');

  deviceTable
    .where({
      userId: user.userId
    }).read({
    success: function(results) {
      if (results.length > 0) {
        // Device record was found. Continue normal execution.
        deviceTable.where({
        userID : user.userId}).update({
           //i put this here just because i thought there had to be a response
           success: request.respond(statusCodes.OK, 'Text length must be under 10')
        }) ;
        console.log('updated position ok');

      } else {
        request.execute();
        console.log('Added New Entry',user.userId);
        //request.respond(statusCodes.FORBIDDEN, 'You do not have permission to submit orders.');
      }
    }
  });
}

1 个答案:

答案 0 :(得分:1)

我想你会想要这样的东西:

function insert(item, user, request) {
  item.userId = user.userId;
  var deviceTable = tables.getTable('Devices');
  deviceTable
    .where({
      userId: user.userId
    }).read({
    success: function(results) {
      if (results.length > 0) {
        //We found a record, update some values in it
        results[0].fieldName = X;
        //Update it in the DB
        deviceTable.update(results[0]);
        //Respond to the client
        request.respond(200, results[0]);
        console.log('updated position ok');

      } else {
        //Perform the insert in the DB
        request.execute();
        console.log('Added New Entry',user.userId);
        //Reply with 201 (created) and the updated item
        request.respond(201, item);
      }
    }
  });
}