1

我正在为 windows phone 8、iOs android 创建一个移动应用程序。我正在使用 windows azure 来保存一些配置文件应用程序和一些设备信息。我对 JavaScript 的经验很少,尽管我一整天都把头撞在砖墙上,我想它开始点击了。话虽这么说,您可能会在下面嘲笑我的代码。

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

如果当前没有用户 ID 的任何记录,我正在尝试进行正常插入。

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

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.');
      }
    }
  });
}
4

1 回答 1

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);
      }
    }
  });
}
于 2013-04-12T16:58:53.107 回答