我在Azure移动服务(javascript后端)中有两个表:TableA和TableB。每次在TableA中运行读取查询时,我想更新TableB中的特定行。两个表都有相同的列"日期"用于标识要更新的行。
我试图通过以下代码(添加到TableA中的读取脚本)来实现这一点,这不能更新TableB(如果我将var myTable中的表名更改为TableA,它可以更新TableA) )
function read(query, user, request) {
request.execute({ success: function(results)
{
request.respond();
//retrieve TableB reference
var myTable = tables.getTable('TableB');
myTable.where({
//retrieve TableB specific row by date
Date: results[0].Date
}).read({
success: function(results){
//reference row to be updated in TableB
var tableRef = results[0];
tableRef.Views = tableRef.Views + 1;
//update row in TableB
myTable.update(tableRef);
}});}});}
请问,我该如何解决?
答案 0 :(得分:1)
更新,以下代码有效:
function read(query, user, request) {
request.execute( { success: function(results) {
request.respond();
var countTable = tables.getTable('TableB');
countTable.where({date: results[0].Date}).read({
success: updateCount
});
function updateCount(results) {
if (results.length > 0) {
// tracking record was found. update and continue normal execution.
var trackingRecord = results[0];
trackingRecord.views = trackingRecord.views + 1;
countTable.update(trackingRecord);
} else {
console.log('error updating count');
}
}
}
});};