无法在html中的脚本模块中初始化firebase应用程序
我正在研究一个最小的(概念验证)项目,并使用预告症和firebase进行。
一个html文件,bundler/transpiler免费,感谢 htm 作为JSX替代。
这是相关的代码:
useEffect(() => {
let isSuscribed = true
const initFirebase = async () => {
let initApp = initializeApp || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js')).initializeApp
if (!initializeApp) {
setInitializeApp(initApp)
}
let getDB = getFirestore || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-firestore.js')).getFirestore
if (!getFirestore) {
setGetFirestore(getDB)
}
const fireApp = initApp(firebaseConfig)
const appDB = getDB(fireApp)
if (isSuscribed) {
setFirebase(fireApp)
setDB(appDB)
}
}
if (firebaseConfig)
initFirebase()
.catch(e => {
console.log(e);
})
return () => isSuscribed = false
}, [firebaseConfig])
当 firebaseconfig (json文件)是从< input type =“ file”/>
加载的,它成功地导入了 pinitializeapp < /strong>和 getfirestore 来自提供的CDN。
但是,无论我是使用firebaseconfig数据还是直接返回的项目ID:
firebaseError:firebase.initializeapp中未提供的“ projectID”。
这也不起作用:
initapp({projectID:{projectID: 'project-id'})
全部都在&lt; script type =“模块”&gt;
标签中。
I am working on a minimal (proof of concept) project with Preact and Firebase.
A single HTML file, bundler/transpiler free thanks to HTM as JSX replacement.
Here is the relevant code:
useEffect(() => {
let isSuscribed = true
const initFirebase = async () => {
let initApp = initializeApp || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js')).initializeApp
if (!initializeApp) {
setInitializeApp(initApp)
}
let getDB = getFirestore || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-firestore.js')).getFirestore
if (!getFirestore) {
setGetFirestore(getDB)
}
const fireApp = initApp(firebaseConfig)
const appDB = getDB(fireApp)
if (isSuscribed) {
setFirebase(fireApp)
setDB(appDB)
}
}
if (firebaseConfig)
initFirebase()
.catch(e => {
console.log(e);
})
return () => isSuscribed = false
}, [firebaseConfig])
When firebaseConfig (JSON file) is loaded from an <input type="file"/>
, it successfully imports initializeApp and getFirestore from the provided CDN.
But no matter if I use firebaseConfig data or directly the project id it returns:
FirebaseError: "projectId" not provided in firebase.initializeApp.
This doesn't work either:
initApp({ projectId: 'project-id' })
All is inside a <script type="module">
tag.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。
我认为代理变量 initapp 正在引起问题。
我改变了 initizeapp 的进口方式,这是一个技巧。
Problem solved.
I think the proxy variable initApp was causing the issue.
I changed the way initializeApp was being imported, and it did the trick.