javascript - 如何将字符串值传递给firebase child()?

标签 javascript firebase firebase-realtime-database

它的工作原理如下:

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()
  });
})

我需要将数据保存到Firebase,我的代码如下:

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.");

}

非常感谢任何帮助。

最佳答案

试试这个

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);
      });
}

关于javascript - 如何将字符串值传递给firebase child()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991952/

相关文章:

javascript - Firebase child_changed 与 equalTo

google-app-engine - Cloud Functions for Firebase 免费套餐中的 “No Outbound Networking” 到底是什么意思?

javascript - 在 JavaScript 中只使用函数的一个参数

javascript - React Native - PropTypes.bool 中的 "Cannot read property ' bool' 未定义

swift - Firebase/Firestore - 完成 block 未按预期工作

javascript - 退出 firebase 身份验证

javascript - 微调我的灯箱

javascript - 更改 knockout 中切换按钮的名称

javascript - 仅调用众多 Firebase 函数之一

ios - Firebase:如何为不同的用户提供读/写访问权限?