如何在调用next函数之前等待firebase.database().ref('users /'+ userId).set()完成? [英] How to wait for firebase.database().ref('users/' + userId).set() to finish before calling next function?

查看:108
本文介绍了如何在调用next函数之前等待firebase.database().ref('users /'+ userId).set()完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数writeUserData(uid,fn, ln,userEmail){
firebase.database()。ref('users /'+ uid).set({
firstName:fn,
lastName:ln,
email: userEmail
});
};

一旦writeUserData完成,我想将用户重定向到不同的页面:

  window.location.href =./mypage.html; 

据我所知,这可以通过 promises ,但是我真的可以用一个很好的例子来解决这个问题。根据firebase文档:
解决方案

数据库/网络/读写#receive_a_promiserel =nofollow noreferrer> https://firebase.google.com/docs/database/web/read-and-write#receive_a_promise set 方法返回一个promise。在这种情况下,最简单的例子就是:
$ b $ $ $ $ $ $ $ $ $ $ $ $函数writeUserData(uid,fn,ln,userEmail){
firebase.database()。ref('users /'+ uid).set({
firstName:fn,
lastName:ln,
email:userEmail
}) .then(function onSuccess(res){
window.location.href =./mypage.html;
})。catch(function onError(err){
// do sth ,例如console.error(err);
});
};

您的代码与我的示例之间的区别在于我添加的部分:

  .then(function onSuccess(res){
window.location.href =./mypage.html;
catch(function onError(err){
// do sth
});

简而言之,如果请求成功,请执行 onSuccess 。如果不是 - 做code onError
$ b

Promise mdn docs






为了提高可读性,您可以这样写:

  function writeUserData(uid,fn,ln,userEmail){
firebase.database()。ref('users /'+ uid).set({
firstName:fn,
姓氏:ln,
email:userEmail
))
.then(onSuccess)
.catch(onError);
};

函数onSuccess(res){
window.location.href =./mypage.html;
}

函数onError(err){
// do sth
}


I'm using the following asynchronous function to write data to my firebase instance:

function writeUserData(uid, fn, ln, userEmail) {
  firebase.database().ref('users/' + uid).set({
    firstName: fn,
    lastName: ln, 
    email: userEmail
  });
};

Once writeUserData has finished, then I want to redirect the user to a different page:

window.location.href = "./mypage.html";

As far as I can understand, this can be done with promises but I could really use a good example to wrap my head around this.

解决方案

According to firebase docs: https://firebase.google.com/docs/database/web/read-and-write#receive_a_promise, the set method returns a promise. In that case, the simplest example for you would be:

function writeUserData(uid, fn, ln, userEmail) {
  firebase.database().ref('users/' + uid).set({
    firstName: fn,
    lastName: ln, 
    email: userEmail
  }).then(function onSuccess(res) {
    window.location.href = "./mypage.html";
  }).catch(function onError(err) {
    // do sth, e.g. console.error(err);
  });
};

The difference between your code and my example is the parts I've added:

.then(function onSuccess(res) {
  window.location.href = "./mypage.html";
}).catch(function onError(err) {
  // do sth
});

which in short means, "if request was successful, do onSuccess. If not - do onError.

You can get more info about how Promises work here: Promise mdn docs


To improve readability you could write it this way:

function writeUserData(uid, fn, ln, userEmail) {
  firebase.database().ref('users/' + uid).set({
    firstName: fn,
    lastName: ln, 
    email: userEmail
  })
  .then(onSuccess)
  .catch(onError);
};

function onSuccess(res) {
  window.location.href = "./mypage.html";
}

function onError(err) {
  // do sth
}

这篇关于如何在调用next函数之前等待firebase.database().ref('users /'+ userId).set()完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆