从导入模块中的函数返回变量

时间:2019-04-02 13:42:09

标签: javascript node.js webpack

我正在使用webpack设置Framework7项目,并且我希望使用Firebase的数据库请求模块。导入的模块中的函数运行,但是所请求的数组从不返回/ promise无法解析。我在做什么错了?

我怀疑在导入模块本身时做错了什么,或者我没有正确解决承诺。我在模块的getAllRestaurants函数中尝试使用 return resolve 都失败了。我尝试使用 async / await .then()甚至使用 setTimout 等待数据库调用的响应。我也试过只是说 let array = database.getAllRestaurants()

database.js(模块)

  // Initialize Firebase
  import firebase from 'firebase'
  import config from '../config';
  const firebaseApp = firebase.initializeApp(config);
  const db = firebase.firestore(firebaseApp);

//getting array form database

export function getAllRestaurants(){
    //defining db route omitted
    let array = [];
    route.get().then(function(querySnapshot){
        querySnapshot.docs.forEach(function(document){
            array.push(document.data());
        });
        return array; //Have tried with only return and with only resolve
    })
    .resolve(array)  //Have tried with only return and with only resolve
    .catch(function(error){
        console.error('error i firebase getallrestaurants: ', error);
    });
}
//The function getAllRestaurants works and gets the array just fine, so the error is in returning it to the function caller

app.js(主要)

//importing my module
import * as database from './database';

//trying to return an array from a function in the module
let array = database.getAllRestaurants(); //returns undefined

//another attempt at getting the same array, .then never executes
database.getAllRestaurants().then((array =>{
  //do stuff
}));

我希望app.js中的代码能够从函数中获取数组,但只会得到'undefined'

2 个答案:

答案 0 :(得分:1)

getAllRestaurants中没有return语句。试试

export function getAllRestaurants(){
    //defining db route omitted
    let array = [];
    return route.get().then(function(querySnapshot){

并跳过这一行:

    .resolve(array)  //Have tried with only return and with only resolve

答案 1 :(得分:0)

我会将您的代码更改为此:

export function getAllRestaurants(){
    // Return a new Promise, which will either resolve a value or reject an error.
    return new Promise(function(resolve, reject){
        //defining db route omitted
      let array = [];
      // Nothing changed here.
      route.get().then(function(querySnapshot){
          querySnapshot.docs.forEach(function(document){
              array.push(document.data());
          });
          resolve(array);
          // Replaced the below line with the one above.
          //return array; //Have tried with only return and with only resolve
      })
      // No need for the line below.
      // resolve(array)  //Have tried with only return and with only resolve
      .catch(function(error){
          console.error('error i firebase getallrestaurants: ', error);
          // Added a line here to reject the error.
          reject(error);
      });
    });
}

我已经评论了上面的编辑。这样,您将始终从该方法中获得一个Promise,并且可以通过以下方式使用它:

//importing my module
import * as database from './database';

database.getAllRestaurants().then(array =>{
  //do stuff
}).catch(err => {
   console.log('an error happened!');
});