Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure移动服务javascript从插入函数更新_Javascript_Mobile_Azure - Fatal编程技术网

Azure移动服务javascript从插入函数更新

Azure移动服务javascript从插入函数更新,javascript,mobile,azure,Javascript,Mobile,Azure,我正在为WindowsPhone8、iOs和android创建一个移动应用程序。我正在使用WindowsAzure来保存一些配置文件应用程序和一些设备信息。我对JavaScript几乎没有什么经验,尽管在我的头撞了一整天的砖墙之后,我想它开始发出咔哒声了。尽管如此,您可能会嘲笑我下面的代码 下面是一个名为Devices的表的insert语句 如果用户ID当前没有任何记录,我将尝试进行正常插入 如果已经有记录,则更新该记录 function insert(item, user, request)

我正在为WindowsPhone8、iOs和android创建一个移动应用程序。我正在使用WindowsAzure来保存一些配置文件应用程序和一些设备信息。我对JavaScript几乎没有什么经验,尽管在我的头撞了一整天的砖墙之后,我想它开始发出咔哒声了。尽管如此,您可能会嘲笑我下面的代码

下面是一个名为Devices的表的insert语句

如果用户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.');
      }
    }
  });
}

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

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

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

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

你的问题是什么?你的问题是什么?