I have a quite simple database in Firestore (geographic name, lat and Lon). The database is very static and I only need to add or remove a record (document) once a while.
How can I add a record manually to the collection, with the same fields as the other documents? When I press "Add document" the console asks me to input each field (see screenshot).
下面的 HTML 页面将允许您写信给您的spots
Firestore 集合。
当然,您需要调整这些字段以及 Firebase 配置。
如果您想进行身份验证,只需添加两个额外的字段,例如用户名和密码并使用signInWithEmailAndPassword() https://firebase.google.com/docs/reference/js/firebase.auth.Auth?authuser=0#signinwithemailandpassword方法。 (如果您愿意,我可以调整页面)。
例如,您可以利用 SSL 证书在 Firebase 托管中托管此页面。或者您可以简单地将其保存在计算机上并使用浏览器打开它(在本例中不是 HTTPS,但这是一种很好的测试方法)。
<!DOCTYPE html>
<html>
<head>
<title>Firebase Form</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-app.js"></script>
<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-auth.js"></script>
</head>
<body>
<div>
<p>Name:</p>
<input type="text" placeholder="Name" id="name" />
<p>City:</p>
<input type="text" placeholder="City" id="city" />
<br /><br />
<input type="submit" value="submit" class="submit" id="submit" />
</div>
<script>
$(document).ready(function() {
// Initialize Firebase
var config = {
apiKey: 'xxxxxxxxxxxxx',
authDomain: 'xxxxxxxxxxxxx',
databaseURL: 'xxxxxxxxxxxxx',
projectId: 'xxxxxxxxxxxxx'
};
firebase.initializeApp(config);
var database = firebase.firestore();
$('#submit').on('click', function() {
var nameValue = $('#name').val();
var cityValue = $('#city').val();
var dataObject = {
name: nameValue,
city: cityValue
};
database.collection('spots').add(dataObject);
});
});
</script>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)