首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将输入数据推入我的Firebase数据库

无法将输入数据推入我的Firebase数据库
EN

Stack Overflow用户
提问于 2017-08-06 15:52:42
回答 2查看 932关注 0票数 0

我目前正在为我们的项目注册模块,这是我第一次使用防火墙作为后端编程。以下是HTML中我的注册表单的代码:

代码语言:javascript
复制
<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中触发此功能:

代码语言: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时所做的所有程序:

  1. 我在firebase中创建了一个帐户
  2. 我在我的电脑上安装了Node.js
  3. 我在HTML文件中放置了以下代码片段(在body部分的底部,在所有其他javascript之前)

代码语言:javascript
复制
<!-- 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');

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-06 16:04:09

ref.push()只返回另一个引用,它不更新,您必须获取新的引用键,然后在父级更新,下面是firebase的示例。

代码语言:javascript
复制
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);
}

将数据读写到防火墙中

票数 0
EN

Stack Overflow用户

发布于 2017-08-06 19:27:56

您可以使用firebase.initializeApp(config)初始化您的防火墙应用程序,这将返回您的应用程序的引用,稍后您可以使用该引用连接到firebase数据库。

代码语言:javascript
复制
var fb = firebase.initializeApp(config);

使用存储在fb上的防火墙引用连接到firebase db,您可以使用

代码语言:javascript
复制
fb.database()

如果要存储注册数据,可以使用firebase 推()方法。

代码语言:javascript
复制
fb.database().ref('tailors').push({
        tName: name,
        tAddreass: address,
        tDescription: description,
        tContact: contact,
        tUsername: username,
        tPassword: pword });

这将在您的防火墙数据库中创建名为create的子程序,并存储与推送Id相对应的注册数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45533800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档