首页 > 解决方案 > 从数据库更新值。反应原生

问题描述

我需要从数据库(couchdb/pouchdb)更新一些值。

我有这个结构:身份验证页面 StartingConsumer(我在其中检查用户是否已登录)如果用户已登录,他将被重定向到 HomepageUtente。

在 HomepageUser 中,我有 FirstName 和 LastName 变量,还有一个文本“Modifica Profilo”来更改 pouchdb 中的一些值。

现在的问题是,如果我通过“Modifica Profilo”更改值,则 HomepageUser 中的值(名字和姓氏不会改变)。

我已经评论了我之前使用的代码。

使用新代码时,我遇到的问题是没有任何变化,因此值“FirstName”和“LastName”不会改变。

您认为错误在哪里?谢谢你。

启动Consumer.js

class StartingConsumer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true
    };
  }

  componentWillMount() {
    Utente.getUtenteLoggato()
      .then(dataUtenteLoggato => {
        if (dataUtenteLoggato !== null) {
          global.utente = new Utente(JSON.parse(dataUtenteLoggato));
          //Actions.homepageutente()({ type: "reset" });
          Actions.homepageutente({ utente: global.utente });

        } else {
          Actions.loginconsumer();
        }
      })
      .catch(err => {
        console.log(err);
      })
      .finally(() => {
        this.setState({ loading: false });
      });
  }

主页Utente

class HomepageUtente extends Component {
    constructor(props) {
        super(props);
    }


    render() {
        const utentelogged = this.props.utente;
        console.log("Utente Logged" + utentelogged)
        //const FirstName = global.utente.data.Person.FirstName;
        const FirstName = utentelogged.data.Person.FirstName;
        //const LastName = global.utente.data.Person.LastName;
        const LastName = utentelogged.data.Person.LastName;
        //const Username = global.utente.data.Person.FiscalCode;
        const Username = utentelogged.data.Person.FiscalCode;

        //const Roles = global.utente.data.Person.Roles
        const Roles = utentelogged.data.Roles

        return (

            <View style={style.container}>
                <View style={style.page}>
                    <Icon name="user-circle" color="#64c7c0" size={70} onPress={() => Actions.visualizzaprofilo({ cf: Username } )} />
                    <Text
                        style={{ paddingBottom: 15, textAlign: 'center', fontSize: 15, color: '#64c7c0', fontWeight: 'bold' }}
                        onPress={() => Actions.visualizzaprofilo({ cf: Username } )} >Visualizza il Profilo
                    </Text>

                    <Text
                        style={{ textAlign: 'center', fontSize: 20, }}>{"Benvenuto"}
                    </Text>
                    <Text
                        style={{ textAlign: 'center', fontSize: 20, color: '#64c7c0', fontWeight: 'bold' }}>
                        {FirstName} {LastName}
                    </Text>
    <Text
                        style={{ color: '#64c7c0', paddingTop: 20 }}
                        onPress={() => Actions.modificaprofilo({ cf: Username })} >MODIFICA PROFILO</Text>}

ModificaProfilo.js

export default class ModificaProfilo extends Component {
  constructor(props) {
    super(props);
    this.state = {
    /*  firstName: "",
      lastName: "",
      dateOfBirth: "",
      address: "",
      city: "",
      country: "",
      email: "",
      height: "",
      weight: "",
      shoesModel: ""*/
    };
  }

  findUtente(cf) {
    let params = {};
    console.log(cf)
    params = {
      "Person.FiscalCode": cf
    };

    global.utente.db
      .localdb()
      .find({
        selector: params
      })
      .then(response => {
        let utente = response.docs[0];
          utente.Person.FirstName = this.state.FirstName;
          utente.Person.LastName = this.state.LastName;
          utente.Person.City = this.state.City;
          utente.Person.Address = this.state.Address;

console.log("utente" + utente)
        return global.utente.db.localdb().put(utente);

标签: javascriptreact-native

解决方案


推荐阅读