简体   繁体   English

页面重定向时没有数据输入到Firebase数据库中

[英]No data entry into firebase database on page redirection

The above code redirects me to the required page, but doesn't enter data into the database. 上面的代码将我重定向到所需的页面,但没有将数据输入数据库。

If I remove window.location.href command, the data is successfully entered in the database. 如果删除window.location.href命令,则数据已成功输入数据库中。

I want to do both the task together. 我想同时完成这两项任务。 What is the way out? 出路是什么?

var firebasep = firebase.database().ref();
var cool = firebasep.child(i.value).set(j.value);
console.log(cool);
firebasep.set(newData, function(error) {
  alert("New");
  window.location.href="Votingpage.html";
});

You can use promises as mentioned here to figure out when it's safe to redirect the user to Votingpage.html 您可以使用此处所述的Promise来确定何时可以安全地将用户重定向到Votingpage.html

firebasep.set(newData).then(function(data){
  alert("New");
  window.location.href="Votingpage.html";
}).catch(function(error){
  console.log(error);
});

It redirects you to the required page but doesn't enter data into the database because the query failed but you mistakenly put your redirection code inside of the error callback, fix you code up like this to make it clearer to see what's happening 它会将您重定向到所需的页面,但不会由于查询失败而将数据输入数据库,但是您将重定向代码错误地放在了错误回调中,并像这样修复代码,以便更清楚地了解正在发生的情况

firebase.set(newData)
  .then((data) => {
      // redirect here
  })
  .catch((error) => {
     // display error here
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM