TypeError: Cannot read property 'then' of undefined in my custom function

TypeError: Cannot read property 'then' of undefined in my custom function

我正在开发我的 react-app,我现在正在使用 react-redux-firebase。继step。我想让它同步并使用 setState() 方法。

显示错误:

TypeError: Cannot read property 'then' of undefined

dbGetDate.js

const dbGetData = (store, col, doc, col2, doc2) => {
    store.firestore.collection(col).doc(doc).collection(col2).doc(doc2).get().then(data => {
            if (data.exists) {
                return new Promise((resolve, reject) => {
                    if(data.data() != null){
                        resolve(data.data());
                    }
                else{
                    reject("Some error in fetching data");
                }

            })

        }
        else {
            return new Promise((resolve, reject) => {
                reject("not such data in record");
            })
        }
        }
    ).catch(function (error) {
        return ("Ops, there should be an error");
    });
}
export default dbGetData;

myPage.js

componentWillMount(){
        const dbStore = dbConnect;
        dbGetData(dbStore, 'Test', '123', 'Member','001')
        .then(data => {
            console.log(data);
            this.setState({
                firebaseData : data
            })
        });

}

您收到该错误的原因是函数 dbGetData 没有 return 任何内容,因此调用它的结果是 undefined。如果你想让它成为 return 一个 Promise,你必须在顶层创建 Promise:

 const dbGetData = (store, col, doc, col2, doc2) => {
   const promise = new Promise((resolve, reject) => {
     // ... do stuff
   });

   return promise;
 };