I'm setting up a Framework7 project with webpack, and I want a module for database requests using Firebase. The function from the imported module runs, but the requested array is never returned/the promise doesn't resolve. What am I doing wrong?
I suspect that I'm either doing something wrong when importing the module itself, or I'm not resolving the promise properly. I've tried and failed with both return and resolve in the getAllRestaurants function in the module. I've tried waiting for the response from the database call with async/await, .then() and even with a setTimout. I've also tried just saying let array = database.getAllRestaurants().
database.js(module)
// 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 (main)
//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
}));
I expected the code in app.js to get an array from the function, but it only gets 'undefined'