2016-07-30 18 views
0

私は新しい火災基地を習得しようとしていて、ウェブサイトのガイドに従っていますが、私はこの部分を正しく取得できません。データをデータベースに保存するにはどうすればよいですか?現在私のデータベースは空で、何が起こっているのかは、ユーザーがテキストを入力した後、ヒットして保存し、ユーザーがUIdとデータのdateCreatedと共にDBに入力したものを保存する必要があります。ここで私が得たものです:Firebase - データベースにデータを保存

<script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script> 
<script> 
    // Initialize Firebase. Note that I'm not using the real init details in this post but I assure you they are present in my app 
    var config = { 
     apiKey: "myApiKey", 
     authDomain: "myProject.firebaseapp.com", 
     databaseURL: "https://myProject.firebaseio.com", 
     storageBucket: "myProject.appspot.com", 
    }; 
    firebase.initializeApp(config); 
    var database = firebase.database() 

    // A whole bunch of other unrelated functions and stuff here 

    $('#addMeal').click(function() { 
     $('#addBox').slideUp(); 
     var meal = $('#txtAdd'); var uid = $('#uid').val(); 
     // If I try to console.log these two vars up here it does actually fire so I don't think there's something wrong with the jquery. 

     if(meal.val()) { 

     function createMeal(uid, email) { 
      firebase.database().ref('meals/').set({ 
      title: name, 
      user: uid, 
      dateCreated: new Date() 
      }); 
     } 

     } else { 
     $('#addBox').addClass('margin_top_xs alert alert-danger').slideDown(); 
     $('#addMsg').html('<h5 class="centered bold margin_bot_no"> You can\'t enter a blank meal!</h5>'); 
     } 
    }); 

</script> 

また、私は本当に私が使うべき方法を記述しているかわからないことに注意してください、私は何をしたいので、他のデータの上に保存せずにDBへの毎日の食事を保存していますあなたがデータを保存するための別の方法で答えても大丈夫です。

ご協力いただければ幸いです。ありがとう!

答えて

1

食事のリストを保持しようとしているようです。だからではなく、setを呼び出す、データベース内の場所にデータを設定する(これは名前が示すように)、あなたはpush()を呼ぶべきappends the data to a list of data

function createMeal(uid, email) { 
     firebase.database().ref('meals/').push({ 
     title: name, 
     user: uid, 
     dateCreated: new Date() 
     }); 
    } 

私はリンクドキュメントはこれをより詳細に説明するので、私はお勧めしますそれを読む(再)。

+0

ありがとうございますが動作しませんでした。私のデータベースはまだ空です:( – PrintlnParams

+0

追加のデータをリストのような場所に保存するには、push()を使用する必要がありますが、これは多くの問題が原因で発生する可能性があります。 **問題を再現する** minimal ** jsfiddle/jsbinを投稿してください。 –

関連する問題