javascript - firebase.auth().currentUser.uid 显示以前的 uid
问题描述
我正在尝试将用户数据设置到实时数据库中。为此,我想要当前登录用户的 uid。但是当我使用 firebase.auth().currentUser.uid; 我得到了以前的uid。
这是代码:
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "*",
authDomain: "*",
databaseURL: "*",
projectId: "*",
storageBucket: "*",
messagingSenderId: "*",
appId: "*",
measurementId: "*"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
//sign up the user
const auth = firebase.auth();
var user_info_reference = firebase.database();
document.getElementById('register-form').addEventListener('submit', submitInfo);
//signup function
const signupForm = document.querySelector('#register-form');
signupForm.addEventListener('submit', (e) => {
e.preventDefault();
const email = signupForm['email_up'].value;
const password = signupForm['pass_up'].value;
//auth with firebase
auth.createUserWithEmailAndPassword(email, password).then(cred => {
console.log(cred.user);
signupform.reset();
user_id = firebase.auth().currentUser.uid;
}).catch(function(error){
var errorcode=error.code;
var errormsg=error.message;
});
});
//saving user info
//submitting info about name
function submitInfo(e){
e.preventDefault();
//get values
var name = getInputval('name_up')
saveInfo(name);
//get id val
function getInputval(id){
return document.getElementById(id).value;
}
function saveInfo(name){
var new_user_info = user_info_reference.ref('user_info/'+ auth.currentUser.uid);
new_user_info.set({
name: name
});
}
}
不要介意配置中的 (*),我只是不想显示该信息。
解决方案
如果您想在当前用户登录后立即获取他们的 UID,您可以从以下方式获取cred
:
auth.createUserWithEmailAndPassword(email, password).then(cred => {
user_id = cred.user.uid;
...
推荐阅读
- scala - 使用 Spark/Kafka 流处理应用程序进行日志记录
- javascript - MongoDB得到一些数组间隔
- javascript - 使用 JavaScript 关闭 Elementor 弹出窗口
- r - replaceData() 使表显示“未找到匹配记录”。过滤表
- reactjs - 跨组件传递图像元数据
- javascript - Javascript(节点)或Python中有什么方法可以仅提取PDF的表单数据并将其转换为JSON?
- puppet - 如何使用 Puppet 复制包含文件的目录
- amazon-web-services - 具有自定义自定义 Lambda 函数的 CloudFront
- sql - 通过 SQL Pivot 将“NULL”值替换为“0”
- python - Scrapy Spider 分页提前结束