im使用返回数组长度的函数 当我在console.log中打印数组的长度时 它的出现 但是当我把它退回来看它没有显示任何东西!!!! 我不知道为什么! 请帮忙
这是我的代码
getLengthNationalNumber () {
var all_national_numbers = [];
db.transaction((tx) => {
tx.executeSql("select * from service_details where service_name = ? ", [item], (tx, results) => {
for (let i = 0; i < results.rows.length; i++) {
let row = results.rows.item(i);
all_national_numbers[i] = row.national_number;
length_of_national_number = all_national_numbers.length;
return (length_of_national_number);
}
});
});
}
作为回报
<View>
<Text>{this.getLengthNationalNumber}</Text>
</View>
答案 0 :(得分:0)
您没有调用函数。您将需要执行以下操作:
<View>
<Text>{this.getLengthNationalNumber()}</Text>
</View>
或者您可以将函数的返回值保存在状态中,在componentDidMount
中调用它,然后显示状态的值。例如:
class MyComponent extends Component {
...
state = {
length: 0
}
componentDidMount = () => {
this.getLengthNationalNumber()
}
getLengthNationalNumber () {
var all_national_numbers = [];
db.transaction((tx) => {
tx.executeSql("select * from service_details where service_name = ? ", [item], (tx, results) => {
for (let i = 0; i < results.rows.length; i++) {
let row = results.rows.item(i);
all_national_numbers[i] = row.national_number;
length_of_national_number = all_national_numbers.length;
this.setState({length: length_of_national_number})
return (length_of_national_number);
}
});
});
}
作为回报:
<View>
<Text>{this.state.length}</Text>
</View>
答案 1 :(得分:0)
我认为使用状态变量更好
在类似的函数中设置状态变量
this.setState({variableName : variableValue})
并在像这样的渲染函数中使用状态变量
<Text>{this.state.variableName}</Text>
每个状态变量设置时间都将重新渲染您的组件。而且,您可以直接在componentWillMount或componentDidMount函数中编写函数。
答案 2 :(得分:0)
在您的代码中,您错过了方法调用的括号。如果您未提供括号,它将搜索“变量”。
<Text>{this.functionName()}</Text>
答案 3 :(得分:0)
根据您的代码, db.transaction()看起来像一个异步调用,它将返回一个Promise而不是您需要打印的数字。
您可以做的是,可以维护一个状态变量,如以下示例所示:
constructor(props){
super(props)
this.state = {
len = 0
}
this.getLengthNationalNumber = this.getLengthNationalNumber.bind(this)
}
...
getLengthNationalNumber () {
var all_national_numbers = [];
db.transaction((tx) => {
tx.executeSql("select * from service_details where service_name = ? ", [item], (tx, results) => {
for (let i = 0; i < results.rows.length; i++) {
let row = results.rows.item(i);
all_national_numbers[i] = row.national_number;
length_of_national_number += all_national_numbers.length;
}
this.setState({len: length_of_national_number});
});
});
}
...
render(){
return(
<View>
<Text>{this.state.len}</Text>
</View>
)
}
答案 4 :(得分:0)
我认为您的问题是您必须使用绑定功能或使用箭头功能,如下所示:
getLengthNationalNumber = () => {
...
}
并且在for循环中有return语句,您的函数总是在第一次迭代中返回。
答案 5 :(得分:0)
尝试以下解决方案:
constructor(props){
super(props)
this.state = {
len = 0
}
}
componentDidMount(){
this.getLengthNationalNumber()
}
getLengthNationalNumber = () => {
var all_national_numbers = [];
let length_of_national_number = 0
db.transaction((tx) => {
tx.executeSql("select * from service_details where service_name = ? ", [item], (tx, results) => {
for (let i = 0; i < results.rows.length; i++) {
let row = results.rows.item(i);
all_national_numbers[i] = row.national_number;
length_of_national_number += all_national_numbers.length;
}
});
}).then(res => {
this.setState({len: length_of_national_number});
});
}
...
render(){
return(
<View>
<Text>{this.state.len}</Text>
</View>
)
}
如果不是then
上的tx.executeSql
子句