如何在调用next函数之前等待firebase.database().ref('users /'+ userId).set()完成? [英] How to wait for firebase.database().ref('users/' + userId).set() to finish before calling next function?
问题描述
函数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
});
简而言之,如果请求成功,请执行 为了提高可读性,您可以这样写: I'm using the following asynchronous function to write data to my firebase instance: Once writeUserData has finished, then I want to redirect the user to a different page: 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 The difference between your code and my example is the parts I've added: which in short means, "if request was successful, do You can get more info about how Promises work here: Promise mdn docs To improve readability you could write it this way:
这篇关于如何在调用next函数之前等待firebase.database().ref('users /'+ userId).set()完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! onSuccess
。如果不是 - 做code onError
$ b
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
}
function writeUserData(uid, fn, ln, userEmail) {
firebase.database().ref('users/' + uid).set({
firstName: fn,
lastName: ln,
email: userEmail
});
};
window.location.href = "./mypage.html";
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);
});
};
.then(function onSuccess(res) {
window.location.href = "./mypage.html";
}).catch(function onError(err) {
// do sth
});
onSuccess
. If not - do onError
.
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
}