无法将数据保存到firebase数据库

时间:2017-07-21 09:08:54

标签: javascript jquery html firebase firebase-realtime-database

我已尝试过所有内容,但它似乎无法将数据保存到数据库中。这是我的示例代码html:

<div class="form-body">
         <div class="form-row">
          <div class="form-controls">
           <input type="text" name="emails" class="field"  id="email" placeholder="Email Address">
          </div><!-- /.form-controls -->
         </div><!-- /.form-row -->
         <div class="form-row">
          <div class="form-controls">
           <input type="text" name="names" class="field" id="name" placeholder="Names (First, Middle, Last)">
          </div><!-- /.form-controls -->
         </div><!-- /.form-row -->


         <div class="form-row">
          <div class="form-controls">
           <input type="number" name="phonenumber" id="number" class="field"  placeholder="Phonenumber">
          </div><!-- /.form-controls -->
         </div><!-- /.form-row -->

          <div class="form-row">
          <div class="form-controls">
           <input type="text" name="countries" id="country" class="field"  placeholder="Country">
          </div><!-- /.form-controls -->
         </div><!-- /.form-row -->

          <div class="form-row">
          <div class="form-controls">
           <input type="text" name="usernames" id="username" class="field"  placeholder="Username">
          </div><!-- /.form-controls -->
         </div><!-- /.form-row --><div align="center">
    <button  onclick="saveData()" id="submit" >Register</button>
  <br />
  <span id="loading_image" style="display: none;">
        <img src="../images/ajax-loader.gif" alt="Please Wait" width="20" height="11"/>    </span>
  </div>

这就是我在html代码体中初始化firebase的方式:

    <script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
<script>
  var config = {
    apiKey: "AIzaSyBNz5xZ-yLqvTum93rEo2cVYtlJXSyUteg",
    authDomain: "kafe-f422b.firebaseapp.com",
    databaseURL: "https://kafe-f422b.firebaseio.com",
    projectId: "kafe-f422b",
    storageBucket: "kafe-f422b.appspot.com",
    messagingSenderId: "491174380565"
  };
  firebase.initializeApp(config);
</script>
        <script src="js/register.js"></script>

        <!-- jQuery library -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

        <!-- Latest compiled JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

        <script src="js/modernizr-custom.js"></script>
     <!-- jQuery 2.1.4 -->
     <script src="js/jQuery-2.1.4.min.js"></script>
     <!-- Bootstrap 3.3.6 JS -->
     <script src="js/bootstrap.min.js" type="text/javascript"></script>
     <!-- Waypoints JS -->
     <script src="js/waypoints.min.js" type="text/javascript"></script>
     <!-- Kafe JS -->
     <script src="js/kafe.js" type="text/javascript"></script>

我把javascript代码用于将数据保存到名为register.js的javascript文件中的firebase。这是代码:

    var user_email = document.getElementById('email');
var  user_name = document.getElementById('name');
var user_phonenumber = document.getElementById('number');
var  user_country = document.getElementById('country');
var  user_username = document.getElementById('username');
var user_submit = document.getElementById('submit');

$('#submit').click(function() {
var cemail = user_email.value;
var cname = user_name.value;
var cnumber = user_phonenumber.value;
var cusername = user_username.value;
var ccountry = user_country.value;

console.log('email is', cemail);
console.log('name is', cname);


  var myRef = firebase.database().ref('register');
 key=myRef.push().key;

  var newData={
    id:key,
email:cemail,
name:cname,
country:ccountry,
username:cusername,
phone_number:cnumber

  }

   myRef.push(newData);

});

请帮我解决这个问题,我已经尝试过阅读firebase中的所有文档,但我似乎无法将数据保存到数据库中。

1 个答案:

答案 0 :(得分:0)

删除key = ...行并使用myRef.push(newData).then(function (snapshot) {})

push方法返回一个承诺。

还要确保您的数据库规则允许您从该路径中读取。