【问题标题】:react native : what is the way to send data from promise function into const? got some error while try console.logreact native:将数据从 promise 函数发送到 const 的方法是什么?尝试 console.log 时出错
【发布时间】:2020-12-02 00:39:39
【问题描述】:

从“Get_Successor_Zones_From_Table”承诺函数发送数据的方式是什么 进入变量 const 数据? 我只需要“Zone_Name”。

这个数据是一个对象列表,它们看起来像这样: [{"Zone_ID":66,"Zone_Name":"Herzel"},{"Zone_ID":0123,"Zone_Name":"Zelda"}]

async Get_Successor_Zones_From_Table() {
    const Zone_Names = [];
    const promise = new Promise((resolve, reject) => {
      db.transaction((tx) => {
        tx.executeSql(
          'SELECT Successor_Zones FROM tblUserConnectResponseData',
          [],
          (_, result) => {
            var len = result.rows.length;
            for (let i = 0; i < len; i++) {
              let row = result.rows.item(i);
              // console.log();
              const { Successor_Zones } = row;
              Zone_Names.push({
                Successor_Zones,
              });
            }
            resolve({
              isAny: true,
              Zone_Names: row,
            });
          },
          (_, err) => {
            reject(err);
          }
        );
      });
    });
    return promise;
  }

我添加这样的按钮:

<TouchableOpacity
                  onPress={() => {
                    this.setState({ activeIndex: 0 });
                    this.Get_Successor_Zones_From_Table.then((result) => {
                      console.log(result);
                    }).catch((error) => console.log(error));
                  }}
                  activeOpacity={1}
                  style={
                    this.state.activeIndex === 0
                      ? styles.button
                      : styles.button2
                  }
                >
                  <Text style={styles.textButton}>list of places</Text>
                </TouchableOpacity>

【问题讨论】:

标签: javascript reactjs sqlite react-native promise


【解决方案1】:

你的声明

async Get_Successor_Zones_From_Table() {

打电话

this.Get_Successor_Zones_From_Table.then((result) => {
    console.log(result)
}).catch(error => console.log(error))

【讨论】:

  • 我在您的调用示例中添加按钮,并说“找不到变量:Get_Successor_Zones_From_Table”为什么会这样?
  • 因为看起来您正在使用类组件。我更新了我的答案,但我建议你学习反应。您显然与存在差距
  • 我按照你的说法再次编辑我的问题,但出现错误:this2.Get_Successor_Zones_From_Table is not a function...它说它未定义
  • 正如我所说,你需要学习反应。您的组件必须以错误的方式制作。显然是上下文错误
  • 你能告诉我应该工作的组件的工作示例吗?只需使用我的功能添加按钮,我就会发现我的方式有什么问题
【解决方案2】:
  const [zoneNames, setZoneNames] = useState();

  const Get_Successor_Zones_From_Table = () => {
    let Zone_Names = [];
    return new Promise((resolve, reject) => {
      db.transaction(tx => {
        // ...
      });
    });
  };

然后

Get_Successor_Zones_From_Table.then(data => setZoneNames(data));

【讨论】:

  • 你能帮帮我吗?我编辑了我的问题,但它对我不起作用,你想我发送我的组件吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多