数据未添加到数据库(firebase)

时间:2019-03-29 11:26:31

标签: javascript html5 firebase firebase-realtime-database

我已经编写了将要约添加到要约下的数据库中的代码,但是它不起作用,并且要约也没有添加到数据库中。我不知道我在哪里弄错了。请帮我找出解决办法

addoffer.html

<html>
<head>   
        <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase.js"></script> 
</head>
<body>
        <form enctype="multipart/form-data">

                        <label>Title</label>
                        <input type="text" id="title" placeholder="Company" >

                        <label for="exampleInputEmail1">Redeem Steps</label>
                        <input type="text" class="form-control" id="steps" placeholder="Redeem Steps">




                        <label>Description</label>
                        <textarea rows="5" class="form-control" id="description" placeholder="Here can be your description"></textarea>



                    <button type="submit" class="btn btn-info btn-fill pull-right" onclick="submitclick()">Add Offer</button>

        </form>
<script>
        // Initialize Firebase
        var config = {
          apiKey: "AIzaSyBMC7E0Q2HmqxDnA-yqSslIOhOijC6LL4s",
          authDomain: "sample-51bb2.firebaseapp.com",
          databaseURL: "https://sample-51bb2.firebaseio.com",
          projectId: "sample-51bb2",
          storageBucket: "sample-51bb2.appspot.com",
          messagingSenderId: "917745166311"
        };
        firebase.initializeApp(config);
      </script>
       <script src="addoffer.js"></script>  
</body>    
</html>

addoffer.js

function submitclick()
{

var titled = document.getElementById("title").value;
var stepsd = document.getElementById("steps").value;
var descriptiond = document.getElementById("description").value;
var id=1;

console.log(titled);
console.log(stepsd);
console.log(descriptiond);

//var firebaseheadingRef = firebase.database().ref().child("offers");


firebase.database().ref('offers/'+id).set({
    title : titled,
    redeem : stepsd,
    description : descriptiond
  }, function(error) {
    if (error) {
      // The write failed...
      console.log(error);
      alert(error);
    } else {
      // Data saved successfully!
    }
  });

  id++;
}

我已经添加了手动添加商品价值的数据库图像

enter image description here

2 个答案:

答案 0 :(得分:0)

更改此:

<head>   
    <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase.js"></script> 
</head>

对此:

<head>
  <!-- Firebase App is always required and must be first -->
  <script src="https://www.gstatic.com/firebasejs/5.9.2/firebase-app.js"></script>

  <!-- Add additional services that you want to use -->
  <script src="https://www.gstatic.com/firebasejs/5.9.2/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.9.2/firebase-database.js"></script>
<head>

答案 1 :(得分:0)

由于您的package ssh; import com.jcraft.jsch.*; import java.io.InputStream; public class Shell{ public static void main(String[] arg){ try{ JSch jsch = new JSch(); Session session = jsch.getSession(arg[0],arg[2] , 22); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setPassword(arg[1]); session.setConfig(config); session.connect(); String command = "configure"; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; System.out.print(new String(tmp, 0, i)); } if (channel.isClosed()) { System.out.println("exit-status: " + channel.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) { } } channel.disconnect(); session.disconnect(); } catch (Exception e) { System.out.println("sdfdfdf"); System.out.println(e.getMessage()); } } } 函数的代码,数据正确已正确添加到数据库中,但ID为1

每次调用此函数时,都将值submitclick()分配给局部变量(对于该函数)1,因此每次写入(和覆盖),因为您使用了{{3} }方法)到id节点。

函数结尾的offers/1无效:下一个呼叫ID设置为1。

您可能应该使用id++方法,该方法将为您的记录生成一个新的ID(请参见set())。

您可以在页面级别使用全局push()变量,但是在这种情况下,您可能会有不同的用户使用相同的id,因此我们建议您使用{{1 }}方法。