javascript - Azure 移动服务 javascript 通过插入功能更新

标签 javascript mobile azure

我正在为 Windows Phone 8、iOs android 创建移动应用程序。我使用 Windows azure 来保存一些配置文件应用程序和一些设备信息。我对 JavaScript 的经验很少,尽管在我把头撞到砖墙上一整天后,我认为它开始点击了。话虽如此,您可能会 mock 我下面的代码。

这(如下)是名为 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.');
      }
    }
  });
}

最佳答案

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

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);
      }
    }
  });
}

关于javascript - Azure 移动服务 javascript 通过插入功能更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627114/

相关文章:

javascript - Lodash _.some 不工作

javascript - 移动(Android)jquery onclick 设计

css - 如何为不同的移动网站制作 CSS

azure - Visual Studio 2017 - 部署到 Azure Function v2 (.NetCore v2.1) 错误 : required process (“Web Management Service” ) is started

asp.net - 如何在 Azure 中编辑网站应用程序设置

javascript - 如何使用 WebRTC 停止屏幕共享?

javascript - 使用变量显示文本框

javascript - 渲染完成时的 Echarts 事件

iphone - 在 Web 应用程序中禁用文本区域的 iPhone 键盘

Azure - 更改了数据库的管理员密码,现在无法访问数据库