我一直在正确地遵循文档,甚至是说明其工作原理的视频,但我尝试并甚至复制了文档中的确切代码几次却无法在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>
这是我尝试运行该程序时的示例日志。
答案 0 :(得分:2)
更改此:
// 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