I am trying to initialize firebade using a json returned by axios
for that I am implementing asynchronous functions with async/await
.
This is my code:
async function init() {
try {
var _app = await axios.post('/app');
var config = _app.data;
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
} catch (e) {
Sentry.captureException(e);
}
return {
storage: function() {
return firebase.storage();
},
database: function() {
return firebase.firestore();
}
};
}
var app = init();
export default app;
But I get these errors when executing the code
app.storage() is not a function
app.database() is not a function
It should be noted that it is a module that I am exporting with webpack for a web environment
An async function always returns a promise.
And that promise doesn't have the properties of the object it will resolve to. To access it you must handle the resolution in the component that firebase will use by invoking it with
await
or with
For example:
import app from './firebase_factory.js';
Try the following way, it may solve your problem:
In the file where you want to run, simply import as follows: