Javascript 自动递增Firebase实时数据库中的共享计数器

Javascript 自动递增Firebase实时数据库中的共享计数器,javascript,firebase,firebase-realtime-database,transactions,Javascript,Firebase,Firebase Realtime Database,Transactions,我需要减少Firebase实时数据库中存储的计数器(名为credits) 要减小计数器的值,请执行以下操作: var ref = admin.database().ref('licenseCredits/' + name + '/credits'); ref.transaction( (value) => { if (value === null) { return 0; } else if (typeof value === 'number') { retu

我需要减少Firebase实时数据库中存储的计数器(名为credits)

要减小计数器的值,请执行以下操作:

var ref = admin.database().ref('licenseCredits/' + name + '/credits');

ref.transaction( (value) => {

  if (value === null) {
    return 0;
  } else if (typeof value === 'number') {

    return value - 1;
  } else {
    console.log('The counter has a non-numeric value: '); 
  }
});
“信用”字段正在正确递减。
我将这段代码放入一个可调用函数中,但我不知道如何将递减值返回给调用者。如果我只是返回ref.transaction结果,我会得到一个“Unhandled RangeError exception”。

根据下面的文档,应该实现一个onComplete函数


根据下面的文档,应该实现一个onComplete函数


最后,考虑到@chris answer,我找到了解决问题的方法

我使用了使用“kew”库实现的javascript承诺

以下是工作代码:

   var qTrans = Q.defer();

    var ref = admin.database().ref('licenseCredits/' + name + '/credits');
    var credits = 0;



    ref.transaction( (value) => {

      if (value === null) {
        // the counter doesn't exist yet, start at one
        return 1;
      } else if (typeof value === 'number') {
        // increment - the normal case
        return value + 1;
      } else {
        // we can't increment non-numeric values
        console.log('The counter has a non-numeric value: ' + JSON.stringify(value));
        // letting the callback return undefined cancels the transaction
      }
    }, (error, committed, snapshot) => {
        if (error) {
            console.log('Transaction failed abnormally!', error);
            qTrans.reject(error);

        } else if (!committed) {
            console.log('We aborted the transaction.');
            qTrans.reject(error);

        } else {
            console.log('Success!');
            console.log("Credit data: ", snapshot.val());
            qTrans.resolve(snapshot.val());

        }
    });

    return qTrans.promise;

最后,考虑到@chris answer,我找到了解决问题的方法

我使用了使用“kew”库实现的javascript承诺

以下是工作代码:

   var qTrans = Q.defer();

    var ref = admin.database().ref('licenseCredits/' + name + '/credits');
    var credits = 0;



    ref.transaction( (value) => {

      if (value === null) {
        // the counter doesn't exist yet, start at one
        return 1;
      } else if (typeof value === 'number') {
        // increment - the normal case
        return value + 1;
      } else {
        // we can't increment non-numeric values
        console.log('The counter has a non-numeric value: ' + JSON.stringify(value));
        // letting the callback return undefined cancels the transaction
      }
    }, (error, committed, snapshot) => {
        if (error) {
            console.log('Transaction failed abnormally!', error);
            qTrans.reject(error);

        } else if (!committed) {
            console.log('We aborted the transaction.');
            qTrans.reject(error);

        } else {
            console.log('Success!');
            console.log("Credit data: ", snapshot.val());
            qTrans.resolve(snapshot.val());

        }
    });

    return qTrans.promise;

很好,我在控制台上打印了正确的信用数据。但是我应该添加什么来将信用值返回给调用者(在我的例子中是android应用程序)。在我的例子中,这段代码在一个可调用的云函数中。这很好,我在控制台上得到了正确的信用数据。但是我应该添加什么来将信用值返回给调用者(在我的例子中是android应用程序)。在我的例子中,这段代码在一个可调用的云函数中。