Javascript 异步等待承诺的正确输出?
我正在使用firstore,我从firstore获得信息 在这里,我想从firstore中检索数据并将其打印出来,但异步模式存在一个问题 输出为未定义后的输出 我想知道如何正确地打印它 接收数据时,函数接收数据,以及在搜索firestore后接收数据的方法 内反应组分Javascript 异步等待承诺的正确输出?,javascript,google-cloud-firestore,Javascript,Google Cloud Firestore,我正在使用firstore,我从firstore获得信息 在这里,我想从firstore中检索数据并将其打印出来,但异步模式存在一个问题 输出为未定义后的输出 我想知道如何正确地打印它 接收数据时,函数接收数据,以及在搜索firestore后接收数据的方法 内反应组分 index = (date) =>{ let date_total = UserAction.getIndex(date) return date_total } render
index = (date) =>{
let date_total = UserAction.getIndex(date)
return date_total
}
render(){
const {user_list} = this.props;
console.log(this.state)
const date = "2020-02-24"
console.log("data",date)
let index = this.index(date) < this
console.log("index", index)
return(...)
index=(日期)=>{
let date_total=UserAction.getIndex(日期)
返回日期合计
}
render(){
const{user_list}=this.props;
console.log(this.state)
施工日期=“2020-02-24”
console.log(“数据”,日期)
让index=this.index(date)
和用户操作函数
export function getIndex(data){
let time_now = moment(data).utc().format()
const user_history_start = moment(time_now).startOf("d").utc().format();
const user_history_end = moment(time_now).endOf("d").utc().format();
let db = loadFB().firestore();
let query = db.collection('users').where('create_date','>=',user_history_start).where('create_date','<=',user_history_end);
let number ;
return query.get().then( docs=>{
number = docs.size
return number
})
}
导出函数getIndex(数据){
现在让时间=时刻(数据).utc().format()
const user_history_start=moment(time_now).startOf(“d”).utc().format();
const user_history_end=moment(time_now).endOf(“d”).utc().format();
设db=loadFB().firestore();
让query=db.collection('users')。其中('create_date','>=',user_history''u start)。其中('create_date','我想您无法在渲染中打印承诺的值,请设置状态并改为打印
constructor() {
super();
this.state = {
index: null
}
}
getIndex = (date) => {
// --------- update ----------
UserAction.getIndex(date).then(date_total => this.setState({ index: date_total }))
}
componentDidMount() {
const date = "2020-02-24"
// --------- update ----------
this.getIndex(date)
}
render() {
console.log("index", this.state.index)
// will first print null,
// then print the index when the promise is done (re-rendered due to state change)
}
您可能希望阅读作为参考。我尝试了我建议的两种方法,但我想要的输出不可用,挂起的是输出@이창훈 有帮助吗?
data 2020-02-24
index promise{<pending>} < (firestore given data)
constructor() {
super();
this.state = {
index: null
}
}
getIndex = (date) => {
// --------- update ----------
UserAction.getIndex(date).then(date_total => this.setState({ index: date_total }))
}
componentDidMount() {
const date = "2020-02-24"
// --------- update ----------
this.getIndex(date)
}
render() {
console.log("index", this.state.index)
// will first print null,
// then print the index when the promise is done (re-rendered due to state change)
}