从数据库读取值并保存在变量中

时间:2019-07-10 08:23:09

标签: javascript react-native

我正在尝试解决此问题,但我不知道该怎么办。

基本上,我有一个数据库,其中包含用户的信息。这些信息可以通过表格进行修改。当用户想要修改值时,将使用用户的FiscalCode在数据库中进行搜索。 找到用户后,我想将旧数据保存在变量中,因为如果我将字段保留为空(或不更改数据),则旧数据已被删除。通过这种方式,我可以使用旧数据。

问题是,如果用户刚刚注册,除了FirstName,LastName和Email之外,他没有其他数据,那么所有其他字段均为空。

问题是当我去读取这些要保存在变量中的数据时,如果它们为空,则我是错误的,因此无法更改。(错误在 readUtente 函数)

我已经尝试过使用这种方法添加“ If”,但是它不起作用。

    componentDidMount(){
    this.readUtente(this.props.cf)
    console.log("this.props.cf: " + this.props.cf)
  }


  //Funzione di prova aggiunta 
  readUtente(cf){
    let params = {};
    params = {
    //  "Person.FiscalCode": this.props.cf
        "Person.FiscalCode": cf
    };
    console.log("1. PARAMS IN MODIFICA PROFILO: " +JSON.stringify(params))
    global.utente.db.localdb().find({
        selector: params
      })
      .then(response => {
      let utente = response.docs[0];
        console.log("2. UTENTE: " + JSON.stringify(utente))
        console.log("3. Utente.Person.FirstName: " + utente.Person.FirstName)
        console.log("4. utente.Person.City: " + utente.Person.City)
        //ho aggiunto if 
       if(utente.Person.FirstName !== undefined) {this.setState({ nome: utente.Person.FirstName })}
       if(utente.Person.LastName !== undefined) {this.setState({ cognome: utente.Person.LastName })}
       if(utente.Person.City !== undefined) {this.setState({ citta: utente.Person.City })} 
       if(utente.Person.Address !== undefined) {this.setState({ indirizzo: utente.Person.Address })}
       if(utente.Person.DateOfBirth !== undefined) {this.setState({ dataNascita: utente.Person.DateOfBirth })}
       if(utente.Person.Country !== undefined) {this.setState({ paese: utente.Person.Country })}
       if(utente.Contacts.Email !== undefined) {this.setState({ email: utente.Contacts.Email })}

      })
      .catch(function(err) {
        console.log(JSON.stringify(err));
      })
  }

  findUtente(cf) {
    console.log("5. Codice fiscale Passato: " + cf)
    let params = {};

    params = {
      "Person.FiscalCode": cf
    };
    console.log("6. PARAMS della findUtente" + JSON.stringify(params))
    global.utente.db
      .localdb()
      .find({
        selector: params
      })
      .then(response => {
        let utente = response.docs[0];
        console.log("7. Utente: " + JSON.stringify(utente))
        console.log("8. this.state.City: " + this.state.citta)
        utente.Person.FirstName = this.state.FirstName == null ? this.state.nome : this.state.FirstName;
        console.log("9. utente.Person.FirstName: " + utente.Person.FirstName)
        utente.Person.LastName = this.state.LastName == null ? this.state.cognome : this.state.LastName;
        console.log("10. utente.Person.LastName: " + utente.Person.LastName)
        utente.Person.City = this.state.City == null ? this.state.citta : this.state.City;
        console.log("11. utente.Person.City: " + utente.Person.City)
        utente.Person.Address = this.state.Address == null ? this.state.indirizzo : this.state.Address;
        utente.Person.DateOfBirth = this.state.DateOfBirth == null ? this.state.dataNascita : this.state.DateOfBirth;
        utente.Person.Country = this.state.Country == null ? this.state.paese : this.state.Country;
        utente.Contacts.Email = this.state.Email == null ? this.state.email : this.state.Email;

        return global.utente.db.localdb().put(utente);
      })
      .catch(function(err) {
        console.log(JSON.stringify(err));
      })
      .finally(function() {
        Actions.homepageutente();
      });
  }

控制台:

    1. PARAMS IN MODIFICA PROFILO: {"Person.FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:34 this.props.cf: ZMBNNZ91P16F364F
ModificaProfilo.js:51 2. UTENTE: {"DocumentType":"GeneralData","Person":{"FirstName":"Nunzio","LastName":"Zambrotti","FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:52 3. Utente.Person.FirstName: Nunzio
ModificaProfilo.js:53 4. utente.Person.City: undefined
ModificaProfilo.js:67 {}
ModificaProfilo.js:72 5. Codice fiscale Passato: ZMBNNZ91P16F364F
ModificaProfilo.js:78 6. PARAMS della findUtente{"Person.FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:86 7. Utente: {"DocumentType":"GeneralData","Person":{"FirstName":"Nunzio","LastName":"Zambrotti","FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:87 8. this.state.City: undefined
ModificaProfilo.js:89 9. utente.Person.FirstName: Nunzio
ModificaProfilo.js:91 10. utente.Person.LastName: Zambrotti
                      11. utente.Person.City: Rome

1 个答案:

答案 0 :(得分:0)

问题可能是空数据没有被标为null,而是空字符串或未定义。

尝试通过以下方式更改if语句:

if(utente.Contacts.Email !== undefined )

if(utente.Contacts.Email !== "" )

if(utente.Contacts.Email)