【问题标题】:Adding two number in Ionic在离子中添加两个数字
【发布时间】:2020-12-18 16:04:28
【问题描述】:

我正在尝试从我的 firebase 实时数据库中添加一个数字,并从 ionic 中添加另一个数字。

ngOnInit() {
    this.number = [50]
  }
  
  check(){

    firebase.database().ref('users/').once('value', (snapshot) => {

    snapshot.forEach(snap => {
      this.newlist.push(snap.val().referralCode); 
      
      if(this.checking.referralCode == snap.val().referralCode){
          console.log('Referral Code Exists')
                   
            this.presentAlert();

            this.auth.onAuthStateChanged(se => {
      
              this.fire = firebase.database().ref('users/' + se.uid).on('value', resp => {
                this.afDB.object('users/'+ se.uid).update({
                  point: parseInt(this.data.point + this.number),
                  
                })
                
              console.log(resp)
              })
            
            }) 
            
            return true;
            
          }
          else{
          console.log('Invalid')
          }
          
         

      })
     
    })

但是结果显示 '050505050505050' 我曾尝试使用 ParseInt() 但这不起作用。

【问题讨论】:

    标签: ionic-framework ionic5


    【解决方案1】:

    我认为您需要在 sum 之前强制转换 this.data.point,如下所示。

    point: +this.data.point + this.number
    

    【讨论】:

    • 我得到一个错误运算符'+'不能应用于类型'number'和'number[]'。当我在那之前放一个+时
    • @JoyCheng 为什么你的this.number = [50] 是一个数组?如果你想要一个简单的数字,你应该把它改成:this.number = 50
    • @JoyCheng 如果你不想用数字替换你的数组,你可以写:+this.data.point + this.number[0]
    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2015-07-14
    相关资源
    最近更新 更多