简体   繁体   English

将 Javascript 的 if…elseif 语句与 Firebase 链接

[英]Linking if…elseif statements of Javascript with Firebase

I want the Income(of database) to store amtV value in Firebase Database when I input the amount for Income.当我输入收入金额时,我希望收入(数据库)将 amtV 值存储在 Firebase 数据库中。 I believe it is possible with if and elseif statements as it works in just reading the values but somehow it doesn't work the same while I want to direct where the amount should go in the database I hope I made my problem clear.我相信 if 和 elseif 语句是可能的,因为它仅在读取值时起作用,但不知何故它不起作用,而我想指示数据库中 go 的数量应该在哪里我希望我能清楚我的问题。

//Insert//
document.getElementById('insert').onclick=function(){
    Ready1();
    firebase.database().ref('Budget/'+dateV1).set({
        Date:dateV1,
        Description:descV,
        if(type ==='inc')
        {
            classSelect=DOMStrings.classSelectInc;
            classSelect = DOMstrings.classSelectInc;
            newHtml = '<div class="item clearfix" id="inc-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>'
            Income:amtV,
        }
        elseif(type=== 'exp'){
            classSelect=DOMStrings.classSelectExp;
            newHtml = '<div class="item clearfix" id="exp-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>'
            Expense:amtV,
        },
        
            });
}

PS: I am new to Programming and Coding. PS:我是编程和编码的新手。

You can't combine the conditional code into a JSON object like that.您不能像这样将条件代码组合到 JSON object 中。

But if you split them, you can accomplish the same:但是如果你拆分它们,你可以完成同样的事情:

document.getElementById('insert').onclick=function(){
    Ready1();
    let value = {
        Date:dateV1,
        Description:descV
    };
    if(type ==='inc')
    {
        classSelect=DOMStrings.classSelectInc;
        classSelect = DOMstrings.classSelectInc;
        newHtml = '<div class="item clearfix" id="inc-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>'
        value.Income = amtV;
    }
    elseif(type=== 'exp'){
        classSelect=DOMStrings.classSelectExp;
        newHtml = '<div class="item clearfix" id="exp-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>'
        value.Expense = amtV;
    },
    firebase.database().ref('Budget/'+dateV1).set(value);
}

So we now take three steps:所以我们现在采取三个步骤:

  1. We build the initial value that we're going to write to the database.我们构建要写入数据库的初始value
  2. We do a condition based on what the user clicks that我们根据用户点击的内容做一个条件
    1. Updates the HTML更新 HTML
    2. Adds a property to the value from step 1.将属性添加到步骤 1 中的value
  3. We write the value to the database.我们将值写入数据库。

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

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