我目前正在为我们的项目注册模块,这是我第一次使用防火墙作为后端编程。以下是HTML中我的注册表单的代码:
<form class="innerContent">
<div class="inputTxt mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="tName">
<label class="inputLabel mdl-textfield__label" for="tName">Name of Tailor/Tailor Shop</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="tAddress">
<label class="inputLabel mdl-textfield__label" for="tAddress">Address of Tailor/Tailor Shop</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield">
<textarea class="mdl-textfield__input" type="text" rows= "3" id="tDesc"></textarea>
<label class="inputLabel mdl-textfield__label" for="tDesc">Description</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield">
<textarea class="mdl-textfield__input" type="text" rows= "3" id="tContact"></textarea>
<label class="inputLabel mdl-textfield__label" for="tContact">Contact Details</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="tUsername">
<label class="inputLabel mdl-textfield__label" for="tUsername">Username</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="password" id="tPassword">
<label class="inputLabel mdl-textfield__label" for="tPassword">Password</label>
</div>
<div class="inputTxt mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="password" id="tRePassword">
<label class="inputLabel mdl-textfield__label" for="tRePassword">Re-type Password</label>
</div>
<p>
<input id="TaC" type="checkbox" for="TaC"> I have read the the <a href="#">Terms and Conditions</a>
<button id="SignUpBtn" class="farBtn mdl-button mdl-js-button mdl-button--raised mdl-button--colored mdl-js-ripple-effect" type="button" onclick="signUp()" disabled>
<i class="material-icons">done_all</i> Sign Up
</button>
</p>
</form>我希望用户将其所有信息注册到我的防火墙数据库中。当按下“注册”按钮时,它将在我的javascript中触发此功能:
function signUp(){
var name = $("#tName").val();
var address = $("#tAddress").val();
var description = $("#tDesc").val();
var contact = $("#tContact").val();
var username = $("#tUsername").val();
var pword = $("#tPassword").val();
var tailors = notsDB.child("tailors");
tailors.push({
tName: name,
tAddreass: address,
tDescription: description,
tContact: contact,
tUsername: username,
tPassword: pword
});但不幸的是,它不起作用!我查看了防火墙控制台中的数据库,没有任何变化。我试图通过将auth != null替换为true来更改规则,但它没有起作用。
撇开没有正确实现push函数的可能性相比,我怀疑我没有正确地连接到我的FIREBASE帐户。我回忆了我在将我的web应用程序连接到firebase时所做的所有程序:
<!-- FIREBASE -->
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase-messaging.js"></script>
<script>
<!-- Initialize Firebase -->
var config = {
apiKey: "AIzaSyCM6Ublqz7PLkRgkzGe6vhsvb7iU0BU5Tk",
authDomain: "nots-eece8.firebaseapp.com",
databaseURL: "https://nots-eece8.firebaseio.com",
projectId: "nots-eece8",
storageBucket: "nots-eece8.appspot.com",
messagingSenderId: "472888671312"
};
firebase.initializeApp(config);
</script>
在我的web应用程序中,是否有我跳过的设置防火墙的程序?是否有必要用npm install firebase --save安装Firebase?还是我的推送函数出错了?
注意:我已经将以下代码放在javascript文件中的signUp方法之前:
var firebase = require('firebase');
var notsDB = new Firebase('https://nots-eece8.firebaseio.com');
发布于 2017-08-06 16:04:09
ref.push()只返回另一个引用,它不更新,您必须获取新的引用键,然后在父级更新,下面是firebase的示例。
function writeNewPost(uid, username, picture, title, body) {
// A post entry.
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
发布于 2017-08-06 19:27:56
您可以使用firebase.initializeApp(config)初始化您的防火墙应用程序,这将返回您的应用程序的引用,稍后您可以使用该引用连接到firebase数据库。
var fb = firebase.initializeApp(config);使用存储在fb上的防火墙引用连接到firebase db,您可以使用
fb.database()如果要存储注册数据,可以使用firebase 推()方法。
fb.database().ref('tailors').push({
tName: name,
tAddreass: address,
tDescription: description,
tContact: contact,
tUsername: username,
tPassword: pword });这将在您的防火墙数据库中创建名为create的子程序,并存储与推送Id相对应的注册数据。
https://stackoverflow.com/questions/45533800
复制相似问题