1

我是 React Js 的新手,我有一个表单,我从用户那里得到一个字符串,我把这个字符串分成空格,然后我把它保存在我的状态变量中,这段代码工作正常:

  handleSubmit(event) {
    event.preventDefault();


    let products = this.state.newProducts;

    //split the string into an array
    var string = this.state.QrCode.split(" ");

    // check all the string in the form if the Qr match an exsitant => save it 
    for(var i =0; i < string.length; i++){    // fetch the entery 
      this.state.smth.map(prd =>  {           // fetch the database
        if(prd.Qr==string[i]){                // verification 
            products.push({
              id: prd.id,
              Qr: prd.Qr,
              Stat: prd.Stat,
              name: prd.name
            });

        }

      }

      )
    }


      // save it in newProducts
    this.setState({
      newProducts: products
    });

    // refresh the grid
    this.grid.refresh();
  }

然后我只是尝试添加变量以在数组中推送数据(如果它不存在“smth”),但出现此错误:解析错误:在严格模式下不允许传统八进制文字。

  handleSubmit(event) {
    event.preventDefault();
    let hasChanged = 0;

    let products = this.state.newProducts;

    //split the string into an array
    var string = this.state.QrCode.split(" ");

    // check all the string in the form if the Qr match an exsitant => save it 
    for(var i =0; i < string.length; i++){    // fetch the entery 
      this.state.smth.map(prd =>  {           // fetch the database
        if(prd.Qr==string[i]){                // verification 
          hasChanged=1;
            products.push({
              id: prd.id,
              Qr: prd.Qr,
              Stat: prd.Stat,
              name: prd.name
            });

        }
        if(hasChanged==0){
          products.push({
            id: 00,
            Qr: string[i],
            Stat: "unknown",
            name: "XXXX"
          });
        }

      }

      )
    }


      // save it in newProducts
    this.setState({
      newProducts: products
    });

    // refresh the grid
    this.grid.refresh();
  }

所以第一个代码工作正常,但它没有添加不匹配的字符串,这就是我想要添加的,希望我能得到一些帮助。谢谢你。

4

0 回答 0