我正在为 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/