如何将 Firebase 与 Wix 连接
How to connect Firebase with Wix
我使用的代码如下:
import firebase from "firebase"
import firestore from "firestore"
export function base() {
// Initialize Firebase
var config = {
apiKey: "apiExample",
authDomain: "authDomaninExample",
databaseURL: "databaseUrlExample",
projectId: "projectIdExample",
storageBucket: "storageBucketExample",
messagingSenderId: "000000000"
};
firebase.initializeApp(config)
var db = firebase.firestore(); // This line breaks the code
db.settings({ timestampsInSnapshots: true })
db.collection("Users")
.add({
test: "Test"
}).then(function (docRef) {
console.log("Document written")
}).catch(function (error) {
console.log("Error is: " + error)
});
}
通过单击按钮调用了 base() 函数,但是代码不起作用,并且没有显示控制台日志。
PS:我根据Wix页面成功安装了Firebase和Firestore节点包
我得到的错误如下:
TypeError: firebase.database is not a function
有效的解决方案如下:
<script src="https://www.gstatic.com/firebasejs/5.9.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.0/firebase-database.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "apiExample",
authDomain: "authDomaninExample",
databaseURL: "databaseUrlExample",
projectId: "projectIdExample",
storageBucket: "storageBucketExample",
messagingSenderId: "000000000"
};
firebase.initializeApp(config)
var db = firebase.firestore(); // This line breaks the code
db.settings({ timestampsInSnapshots: true })
db.collection("Users")
.add({
test: "Test"
}).then(function (docRef) {
console.log("Document written")
}).catch(function (error) {
console.log("Error is: " + error)
});
</script>
但是我不想使用脚本,因为我更喜欢使用 typescript
这可能会有帮助"The short answer is no - Wix Code only supports its internally used database."
更多详情
https://www.wix.com/corvid/forum/community-discussion/is-there-a-way-to-connect-to-firebase-database
wix 论坛中的一位用户似乎提出了一个 hacky 解决方案。
这是link,也许这会对你有所帮助。
https://www.wix.com/corvid/forum/main/comment/5c5a4ffff7055001e2d15cd4
接受的解决方案是有问题的,hacky 等同于脆弱。因此,当它坏掉时,您可能有一段时间没有意识到它,或者它可能经常坏掉。最好的解决方案是使用 wix 和
Using wix-router and wix-fetch you can write code that pulls
information from incoming requests for the profile page, queries an
external database to retrieve the information for the page, and then
injects that data into the profile page
我使用的代码如下:
import firebase from "firebase"
import firestore from "firestore"
export function base() {
// Initialize Firebase
var config = {
apiKey: "apiExample",
authDomain: "authDomaninExample",
databaseURL: "databaseUrlExample",
projectId: "projectIdExample",
storageBucket: "storageBucketExample",
messagingSenderId: "000000000"
};
firebase.initializeApp(config)
var db = firebase.firestore(); // This line breaks the code
db.settings({ timestampsInSnapshots: true })
db.collection("Users")
.add({
test: "Test"
}).then(function (docRef) {
console.log("Document written")
}).catch(function (error) {
console.log("Error is: " + error)
});
}
通过单击按钮调用了 base() 函数,但是代码不起作用,并且没有显示控制台日志。
PS:我根据Wix页面成功安装了Firebase和Firestore节点包
我得到的错误如下:
TypeError: firebase.database is not a function
有效的解决方案如下:
<script src="https://www.gstatic.com/firebasejs/5.9.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.0/firebase-database.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "apiExample",
authDomain: "authDomaninExample",
databaseURL: "databaseUrlExample",
projectId: "projectIdExample",
storageBucket: "storageBucketExample",
messagingSenderId: "000000000"
};
firebase.initializeApp(config)
var db = firebase.firestore(); // This line breaks the code
db.settings({ timestampsInSnapshots: true })
db.collection("Users")
.add({
test: "Test"
}).then(function (docRef) {
console.log("Document written")
}).catch(function (error) {
console.log("Error is: " + error)
});
</script>
但是我不想使用脚本,因为我更喜欢使用 typescript
这可能会有帮助"The short answer is no - Wix Code only supports its internally used database."
更多详情 https://www.wix.com/corvid/forum/community-discussion/is-there-a-way-to-connect-to-firebase-database
wix 论坛中的一位用户似乎提出了一个 hacky 解决方案。
这是link,也许这会对你有所帮助。
https://www.wix.com/corvid/forum/main/comment/5c5a4ffff7055001e2d15cd4
接受的解决方案是有问题的,hacky 等同于脆弱。因此,当它坏掉时,您可能有一段时间没有意识到它,或者它可能经常坏掉。最好的解决方案是使用 wix 和
Using wix-router and wix-fetch you can write code that pulls information from incoming requests for the profile page, queries an external database to retrieve the information for the page, and then injects that data into the profile page