2013-03-26 70 views
1

我正在爲windows phone 8,iOs android創建移動應用程序。我正在使用Windows Azure來保存一些配置文件應用程序和一些設備信息。我對JavaScript的使用經驗很少,儘管我整天把頭撞到一堵磚牆上,然後開始點擊我的想法。這就是說你可能會嘲笑我的代碼如下。Azure移動服務javascript從插入功能更新

這個(下面)是一個名爲Devices的表的insert語句。

即時通訊嘗試做一個正常的插入,如果目前沒有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

什麼是你的問題? – cederlof 2013-04-12 16:19:42

回答

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