Firebase交易无效

时间:2017-12-08 07:01:31

标签: javascript angular firebase firebase-realtime-database angularfire2

我使用angularfire2从角度应用程序调用,this.afDb.database是数据库实例

const downloadsRef = this.afDb.database.ref('research_reports-published/' + rrid + '/' + field);
downloadsRef.transaction(function(fieldval) {
  if (fieldval) {
    fieldval = fieldval + 1;
  }
  return fieldval;
},
function(error, committed, snapshot) {
  if (error) {
    console.log('Transaction failed abnormally!', error);
  } else if (!committed) {
    console.log('We aborted the transaction (because ada already exists).');
  } else {
    console.log('User ada added!');
  }
  console.log('Adas data: ', snapshot.val());
}).then(function() {
    console.log('Transaction successfully committed!');
}).catch(function(error) {
    console.log('Transaction failed: ', error);
});

代码只是静默打印

Adas data: oldvalue 
Transaction successfully committed

并退出

1 个答案:

答案 0 :(得分:0)

在上面的代码中发现了这个问题。它应该是

if (fieldval != null) {
  fieldval = fieldval + 1;
}

as fieldval = 0也使其为假