将数据发送到firebase并返回index.html

时间:2017-05-10 00:43:36

标签: javascript html firebase firebase-realtime-database

我正在使用javascript表单向 Firebase数据库发送一些数据,在我确认数据已保存后,我需要该页面返回index.html,此处为'我得到了什么:

的JavaScript

 btnAsset.addEventListener('click', e =>{
  const name = inputName.value;
  const model = inputModel.value;
  var rootRef = firebase.database().ref().child("Assets");
  rootRef.child(name).child("model").set(model);
});

HTML输入

<input type="text" class="form-control" id="inputName" placeholder="Asset Name">
<input type="text" class="form-control" id="inputResp" placeholder="Owner">

1 个答案:

答案 0 :(得分:2)

您需要使用firebase.Promise方法返回的Reference.set(value)。例如:

rootRef.child(name).child("model").set(model)
    .then(function() {
        console.log('Synchronization succeeded');
        // Return to index.html here...
    })
    .catch(function(error) {
        console.log('Synchronization failed');
    });

最好将此添加到btnAsset听众的末尾(有关详细信息,请参阅Jon Jacques的评论):

e.preventDefault();