【问题标题】:JS Function returning Function instead of valueJS函数返回函数而不是值
【发布时间】:2021-01-10 07:44:40
【问题描述】:

我一直在尝试通过从 Firebase 数据库中获取数据来使用 JS 制作一个简单的数学函数。 我们有两种类型——收入和支出。

这里最初是引用 Firebase 的代码 sn-p


    let reference = firebase.database().ref("expenditure");
    let reference1=firebase.database().ref("income");

这让我们可以创建两个不同的数据库,名称分别为 expenditure 和 income

现在,我创建了两个函数来遍历数据库中的元素并将其添加到变量中

这些函数应该分别返回支出和收入。



var income=0;

var expenditure=0;



function amount(data){
    let expens = data.val();
    let keys = Object.keys(expens);
    console.log(keys.length);

    for( let i=0;i<keys.length;i++){

    let a =keys[i];
    let amount_a = expens[a].Amount;

    var p=parseInt(amount_a);
    var expenditure=p+expenditure;

    }


    return expenditure;
}



function amount1(data){
    let expens = data.val();
    let keys = Object.keys(expens);
    console.log(keys.length);

    for( let i=0;i<keys.length;i++){

    let a =keys[i];
    let amount_a = expens[a].Amount;

    var p=parseInt(amount_a);
    var income=p+income;

    }


    return income;
}

如你所见,我声明了两个函数来返回收入和支出

现在,我在另一个名为 result() 的函数中调用了它们

function result(){
    var a=reference.on("value",amount);
    var b=reference1.on("value",amount1);
    console.log(b-a);
}

result();

这应该返回我净额(收入-支出) 但是,我在 console.log 中获得了与 amount(data),amount1(data) 相同的功能,而不是返回收入和支出的值。

我尝试了几种方法,但无法弄清楚。

Checkout the Console.log here

console.log({a,b})

【问题讨论】:

  • 你确定reference.on("value",amount)返回一个数字吗?你也可以打印控制台像这样console.log({ a, b })
  • 我猜它应该调用 amount() 函数。不然怎么调用 amount() 函数?
  • 图片添加到最后一行的帖子

标签: javascript reactjs firebase-realtime-database


【解决方案1】:

Firebase .on(...) 函数不会从数据库返回值,它会将值发送给作为参数传递的回调函数。

您应该使用回调函数更新变量。假设你的数据库结构是这样的:

(root)
-- expenditure
---- (key)
------- Amount: 0
---- (key)
------- Amount: 0
-- income
---- (key)
------- Amount: 0
---- (key)
------- Amount: 0

那么你的代码应该更像这样:

let expenditureRef = firebase.database().ref("expenditure");
let incomeRef = firebase.database().ref("income");

let expenditures = 0;
let income = 0;

let netAmount = 0;


expenditureRef.on("value", function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
        expenditures += parseInt(childSnapshot.val().Amount);
        netAmount = income - expenditures;
    }
});

incomeRef.on("value", function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
        income += parseInt(childSnapshot.val().Amount);
        netAmount = income - expenditures;
    }
});

我无法对此进行测试,所以如果这不起作用,请随时告诉我,我可以尝试修复它。

我还强烈建议您阅读此处的文档: https://firebase.google.com/docs/database/web/read-and-write

【讨论】:

    【解决方案2】:

    由于reference.on() 返回回调,您只需要像这样修改代码

    function result(){
        var a=reference.on("value",amount)();
        var b=reference1.on("value",amount1)();
        console.log(b-a);
    }
    

    这将返回您想要的值。

    【讨论】:

      【解决方案3】:

      我不知道这是否能解决你的问题,但你应该删除重新声明的支出

      for(...){
          ...
          var expenditure=p+expenditure;
      }
      return expenditure;
      

      您在该 for 循环中定义的支出仅存在于该 for 循环中,不应在该循环之外可用;它的引用应该覆盖全局级别的支出变量。

      我建议删除该行中的var

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 2015-07-03
        • 2016-03-19
        • 1970-01-01
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        相关资源
        最近更新 更多