javascript - 从函数打印值。 react native

标签 javascript react-native

我有一个关于如何在函数中打印值的问题,在 react-native 中。

基本上我有一个函数可以在数据库中研究一些值,我应该打印这些值。

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];
          console.log ("utente: " + utente)
          utente.Person.FirstName;
          console.log ("utente.Person.FirstName: " + utente.Person.FirstName)
        })
        //.... catch...}); }
render() {
   {this.findUtente(this.props.cf)}
return (
      <View style={style.container}>
        <View style={style.page}>
          <KeyboardAwareScrollView>
            <Text style={visualProfilo.text}>Name:</Text>
            <Text style={visualProfilo.text1}>{Stamp Here the FirstName}</Text>

控制台日志中的值以正确的方式打印。 如何打印该值?

谢谢

最佳答案

您可以使用 componentDidMount 调用该方法,然后您可以将值设置为状态,然后使用 setState 更新值,然后呈现状态值 示例:

state = {
   FirstName: ''
}
componentDidMount(){
  this.findUtente(this.props.cf)
}

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];
          this.setState({FirstName: utente.Person.FirstName})
        })
        //.... catch...}); }
render() {
  return (
   //everything remains same
   <Text style={visualProfilo.text1}>{this.state.FirstName}</Text>
  )
}

关于javascript - 从函数打印值。 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56854315/

相关文章:

javascript - 如何将js文件包含到Velocity模板中?

javascript - 更改包装选择的值,该值是由 AngularJs 从 Chrome 内容脚本动态创建的

javascript - 如何按照数组元素的顺序从数组创建对象

ios - 初始化 native ReactLocalization 模块时出错。请检查您的配置 : Did you run 'react-native link' ?

react-native - native 库 : How to render an Icon?

android - 找不到 com.android.tools.build :gradle:2. 3.+ 的任何匹配项,因为没有可用的 com.android.tools.build:gradle 版本

android - 无法在 Android 应用程序中集成 React-Native

javascript - 对 MongoDB 的重复 Node.js 请求最终会变慢

javascript - Socket.io 仅针对发射器发出事件

javascript - javascript es-06 中重写函数时调用父类的函数而不是子类的函数