gpt4 book ai didi

javascript - 如何在网络中使用javascript将数据插入firestore?

转载 作者:行者123 更新时间:2023-12-01 00:22:40 24 4
gpt4 key购买 nike

我正确地遵循了文档,甚至解释了它是如何工作的视频,但是我尝试了多少次,甚至复制了文档中的确切代码,但我无法在 firestore 数据库上插入数据。你能告诉我为什么会发生这种情况吗?另外,我的数据库没有问题,因为我已经将其配置为读取和写入。

下面是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>Login Admin</title>
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity></script>
</head>

<style type="text/css">

</style>

<body>
<section class="container-fluid bg">
<section class="row justify-content-center">
<section class="col-12 col-sm-6 col-md-3">
<form class="form-container">
<div class="form-group">
<label for="codeidtextfield">Code ID:</label>
<input type="text" class="form-control" id="codeidtextfield" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">Code ID's are provided by the Higher Ups. You must request first if you don't have one.</small>
</div>
<div class="form-group">
<label for="passwordfield">Password:</label>
<input type="password" class="form-control" id="passwordfield">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Remember Me</label>
</div>
<button type="submit" class="btn btn-primary btn-block" id="submit_btn">Submit</button>
</form>
</section>
</section>
</section>
</body>
<!-- Firebase CDN -->
<script src="https://www.gstatic.com/firebasejs/7.5.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.2/firebase-firestore.js"></script>
<script>

//This config data are fine, I just removed it for the sake of privacy i guess.
var config = {
apiKey:
authDomain:
databaseURL:
projectId:
storageBucket:
messagingSenderId:
appId:
measurementId:
};
// Initialize Firebase
var app = firebase.initializeApp(config);
// Initialize Cloud Firestore through Firebase
const db = firebase.firestore(app);

const docRef = db.collection("Admin").doc('CourierApplicationId').collection('CodeIDS');
const codeidField = document.querySelector("#codeidtextfield");
const passwordField = document.querySelector("#passwordfield");
const submitButton = document.querySelector("#submit_btn");

submitButton.addEventListener("click", function(){
const codeid = codeidField.value;
const passworddata = passwordField.value;

console.log("I am going to authenticate: "+codeid+" and "+passworddata+" to Firestore database.");

docRef.add({
codeID: codeid,
password: passworddata
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
});
</script>
</html>

这是我尝试运行该程序时的示例日志。

enter image description here

最佳答案

更改此:

    // Initialize Firebase
var app = firebase.initializeApp(config);
// Initialize Cloud Firestore through Firebase
const db = firebase.firestore(app);

进入此:

    // Initialize Firebase
var app = firebase.initializeApp(config);
// Initialize Cloud Firestore through Firebase
const db = app.firestore();

使用firestore()方法连接到firestore:

https://firebase.google.com/docs/reference/js/firebase.app.App.html

关于javascript - 如何在网络中使用javascript将数据插入firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59280121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com