2017-09-01 47 views
0

它的工作原理是這樣的:如何將字符串值傳遞給firebase child()?

var firebaseRef = firebase.database().ref().child(document.getElementById("areaCode").value); 

有什麼建議或通過x這樣的:

var areaCodex = document.getElementById("areaCode"); 
var x = areaCodex.toString(); 
firebase.initializeApp(config); 
var rootRef = firebase.database().ref().child(x); 

$('#saveBtn').click(function() { 
    rootRef.set({ 
    date: $('#date').val(), 
    timeF: $('#fromTime').val() 
    }); 
}) 

我需要保存數據到火力地堡和我的代碼如下:

var toTime = document.getElementById("toTime"); 
var areaCode = document.getElementById("areaCode"); 

function submitClick(){ 
    var firebaseRef = firebase.database().ref().child();//here in .child() i need to pass the areaCode as an argument. It works when i hardcode a value like .child('1234'). But i need that to be what ever the value I am getting from the textbox of areaCode. I have tried //var x = areaCode.value; // var y = x.toString and var y = String(x); , trying to pass y as .child(y) 
// also var y = areaCode.toString(); does not work. 

    var toTimeText = toTime.value; 
    firebaseRef.child("toTime").set(toTimeText); 

    window.confirm("Data has been successfully sent."); 

} 

任何幫助高度讚賞。

回答

1

試試這個

function sumbitClick(){ 
var toTimeText = document.getElementById("toTime").value; 
var ref = firebase.database().ref(path_to_your_child); 

     ref.once("value") 
     .then(function(snapshot) { 
     ref.set(toTimeText); 
     }); 
} 
+0

記住這個代碼應該是在HTML文檔的結尾部分

+0

你好,Antonio Thanx回答。這裏是什麼.ref(path_to_your_child); ? – angels65

+0

我的第一個節點是區號。在那之下我需要有toTime值 – angels65