【发布时间】:2019-03-16 21:17:59
【问题描述】:
我正在做一个项目,我使用 redux 通过 thunk 中间件来管理应用程序。但是,当我从它给我的操作中调用下面的函数时,
Possible Unhandled Promise Rejection (id: 0): Error: Cannot read property 'then' of undefined.
代码
1 export const submitEmail=(email)=>{
2 return(dispatch) =>{ // Still did not use dispatch
3 val = getData(email);
4 console.log(val);
5 }
6 };
7
8 const getData = async(email)=>{
9 let usersRef = await firebase.database().ref('Users')
10 usersRef.orderByChild('email').equalTo(email).on("value", (snapshot) => {
11 snapshot.forEach((snap) => {
12 return snap.val();
13 })});
14 }
因此,当我运行 submitEmail 函数时,val 的值在收到此警告之前给了我一个承诺。
Promise {_40: 0, _65: 0, _55: null, _72: null}
问题
我想等待,直到查询数据库并获取 json 格式的数据。
注意
这是因为firebase.database().ref('Users') 没有返回承诺。但是,在评估 getData 函数之后,如何在 line 4 中获得 var 值?
【问题讨论】:
标签: javascript firebase react-native firebase-realtime-database async-await