繁体   English   中英

JavaScript 对象将值设置为对象但不更新到 firebase

[英]JavaScript object setting values to object but not updating to firebase

有一个名为 reader rank 的节点有 14 个 rank,当我尝试使用静态代码计算点时,它对我来说很好用,这里有一些代码

rank_r = getReaderRank(parseFloat(oldPointsOfReder)); 
  userDataReaderRef.update(rank_r); 

function getReaderRank(points) {
  let obj = {};
  if (points >= 0 && points <= 50) {
    obj.userRankBadge = 'Bronze';
    obj.userRank = 'rank1';
    obj.userPoints = points.toFixed(2);
    obj.commission = 25;
    obj.userRankPrice = 0.49;
  } else if (points >= 51 && points <= 100) {
    obj.userRankBadge = 'Silver';
    obj.userRank = 'rank2';
    obj.userPoints = points.toFixed(2);
    obj.commission = 30;
    obj.userRankPrice = 0.99;
  } else if (points >= 101 && points <= 200) {
    obj.userRankBadge = 'Gold';
    obj.userRank = 'rank3';
    obj.userPoints = points.toFixed(2);
    obj.commission = 35;
    obj.userRankPrice = 1.49;
  } else if (points >= 201 && points <= 400) {
    obj.userRankBadge = 'Platinum';
    obj.userRank = 'rank4';
    obj.userPoints = points.toFixed(2);
    obj.commission = 40;
    obj.userRankPrice = 1.99;
  } else if (points >= 401 && points <= 800) {
    obj.userRankBadge = 'Diamond';
    obj.userRank = 'rank5';
    obj.userPoints = points.toFixed(2);
    obj.commission = 45;
    obj.userRankPrice = 2.49;
  } else if (points >= 801 && points <= 1600) {
    obj.userRankBadge = 'Master';
    obj.userRank = 'rank6';
    obj.userPoints = points.toFixed(2);
    obj.commission = 50;
    obj.userRankPrice = 2.99;
  } else if (points >= 1601 && points <= 3200) {
    obj.userRankBadge = 'Challenger';
    obj.userRank = 'rank7';
    obj.userPoints = points.toFixed(2);
    obj.commission = 55;
    obj.userRankPrice = 3.49;
  } else if (points >= 3201 && points <= 6400) {
    obj.userRankBadge = 'Herald';
    obj.userRank = 'rank8';
    obj.userPoints = points.toFixed(2);
    obj.commission = 60;
    obj.userRankPrice = 3.99;
  } else if (points >= 6401 && points <= 12800) {
    obj.userRankBadge = 'Guardian';
    obj.userRank = 'rank9';
    obj.userPoints = points.toFixed(2);
    obj.commission = 65;
    obj.userRankPrice = 4.49;
  } else if (points >= 12801 && points <= 25600) {
    obj.userRankBadge = 'Crusader';
    obj.userRank = 'rank10';
    obj.userPoints = points.toFixed(2);
    obj.commission = 70;
    obj.userRankPrice = 4.99;

  } else if (points >= 256001 && points <= 51200) {
    obj.userRankBadge = 'Archon';
    obj.userRank = 'rank11';
    obj.userPoints = points.toFixed(2);
    obj.commission = 75;
    obj.userRankPrice = 5.49;
  } else if (points >= 512001 && points <= 100000) {
    obj.userRankBadge = 'Legend';
    obj.userRank = 'rank12';
    obj.userPoints = points.toFixed(2);
    obj.commission = 80;
    obj.userRankPrice = 5.99;
  } else if (points >= 100001 && points <= 150000) {
    obj.userRankBadge = 'Ancient';
    obj.userRank = 'rank13';
    obj.userPoints = points.toFixed(2);
    obj.commission = 85;
    obj.userRankPrice = 7.99;
  } else if (points >= 150001 && points <= 10000000000) {
    obj.userRankBadge = 'Archon';
    obj.userRank = 'rank14';
    obj.userPoints = points.toFixed(2);
    obj.commission = 90;
    obj.userRankPrice = 9.99;
  } else if (points >= 10000000000) {
    obj.userRankBadge = 'Archon';
    obj.userRank = 'rank14';
    obj.userPoints = points.toFixed(2);
    obj.commission = 90;
    obj.userRankPrice = 9.99;
  } else {
    obj.userRankBadge = 'Bronze';
    obj.userRank = 'rank1';
    obj.userPoints = '0';
    obj.commission = 25;
    obj.userRankPrice = 0.49;
  }

  return obj;
}

但是当我试图让它动态地从 firebase 数据库中获取所有点值时,以下函数将所有值设置为对象,但对象未将值更新到数据库

  1. 动态方法

    函数 getReaderRank(points) { 让 obj = {}; let rank = firebase.database().ref("ReaderRanks/").once("value", function(snaps) { snaps.forEach(function(child) { let item = child.val(); let i; for (i = 0; i <= item; i++) { let strtVal = item.startValue; let endVal = item.endValue;

     if (points >= strtVal && points <= endVal) { obj.userRankBadge = item.name; obj.userRank = item.rank_no; obj.userPoints = points.toFixed(2); obj.commission = item.commission; obj.userRankPrice = item.price; } } });

    }); 返回对象; }

但是对象没有将值更新到数据库

使用您问题中的代码,您只是数据库中读取数据(使用once()方法)。

如果要更新数据库中的值则需要使用update()set()方法,具体取决于您的确切需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM