【问题标题】:Send data to firebase and return to index.html将数据发送到 firebase 并返回 index.html
【发布时间】:2017-05-10 00:43:36
【问题描述】:

我正在使用 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">

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database


    【解决方案1】:

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

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

    【讨论】:

    • 另外,您可能希望在 btn 侦听器的末尾添加 e.preventDefault(),因为按钮的默认行为是提交表单。如果没有提交表单,页面将重新加载。根据 OP 描述问题的方式,我想这也会发生在这里。
    • @JonJaques 好收获!已更新答案并感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2019-10-19
    • 2014-10-17
    • 1970-01-01
    • 2013-10-01
    • 2018-03-09
    • 2011-09-30
    相关资源
    最近更新 更多