最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore get access to user ID in other functions - Stack Overflow

programmeradmin1浏览0评论

im creating a app with the firestore database. I have a problem where i want to acces to user id in other functions, how can i do that?

Im using the firebase.auth().onAuthStateChanged function to watch if the user is logged in, and i can acces the user id in here.

How can i access it when i click on the btnName? now i have to following error:

Uncaught ReferenceError: uid is not defined

HTML

<html lang="en">
<head>
  <meta charset="utf-8">

  <title></title>

  <meta name="description" content="">
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, width=device-width, maximum-scale=1.0" />

  <script src=".9.0/firebase.js"></script>
  <script src=".9.0/firebase-firestore.js"></script>

</head>

<body>

    <h1>Wele</h1>
    <button id="btnLogout" class="hide">Logout</button>
    <br><br>
    <input id="txtName" type="" placeholder="Name">
    <button id="btnName">Add name</button>

    <script src="js/app.js"></script>

</body>
</html>

js

// Initialize Firebase
var config = {
  apiKey: "AIzaSyBAQYQxSU0JkskvXKqBKzAHHWViutuP_lg",
  authDomain: "crypto-3171a.firebaseapp",
  databaseURL: "",
  projectId: "crypto-3171a",
  storageBucket: "crypto-3171a.appspot",
  messagingSenderId: "86923986632"
};
firebase.initializeApp(config);

const db = firebase.firestore();

const txtName = document.getElementById('txtName');
const btnName = document.getElementById('btnName');

// log user out of app
btnLogout.addEventListener('click', e => {
    firebase.auth().signOut();
});

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
      addToUsers(user)
  } else {
  }
});

// add users to database 'users'
function addToUsers(user) {
    var email = user.email;
    var uid = user.uid;
    var users = db.collection("users").doc(uid);
    users.set({
        email: email,
        id: uid
    });
}

// when user clicks add name to 'users' document
btnName.addEventListener('click', e => {
    console.log(db.collection("users").doc(uid))
});

im creating a app with the firestore database. I have a problem where i want to acces to user id in other functions, how can i do that?

Im using the firebase.auth().onAuthStateChanged function to watch if the user is logged in, and i can acces the user id in here.

How can i access it when i click on the btnName? now i have to following error:

Uncaught ReferenceError: uid is not defined

HTML

<html lang="en">
<head>
  <meta charset="utf-8">

  <title></title>

  <meta name="description" content="">
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, width=device-width, maximum-scale=1.0" />

  <script src="https://www.gstatic./firebasejs/4.9.0/firebase.js"></script>
  <script src="https://www.gstatic./firebasejs/4.9.0/firebase-firestore.js"></script>

</head>

<body>

    <h1>Wele</h1>
    <button id="btnLogout" class="hide">Logout</button>
    <br><br>
    <input id="txtName" type="" placeholder="Name">
    <button id="btnName">Add name</button>

    <script src="js/app.js"></script>

</body>
</html>

js

// Initialize Firebase
var config = {
  apiKey: "AIzaSyBAQYQxSU0JkskvXKqBKzAHHWViutuP_lg",
  authDomain: "crypto-3171a.firebaseapp.",
  databaseURL: "https://crypto-3171a.firebaseio.",
  projectId: "crypto-3171a",
  storageBucket: "crypto-3171a.appspot.",
  messagingSenderId: "86923986632"
};
firebase.initializeApp(config);

const db = firebase.firestore();

const txtName = document.getElementById('txtName');
const btnName = document.getElementById('btnName');

// log user out of app
btnLogout.addEventListener('click', e => {
    firebase.auth().signOut();
});

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
      addToUsers(user)
  } else {
  }
});

// add users to database 'users'
function addToUsers(user) {
    var email = user.email;
    var uid = user.uid;
    var users = db.collection("users").doc(uid);
    users.set({
        email: email,
        id: uid
    });
}

// when user clicks add name to 'users' document
btnName.addEventListener('click', e => {
    console.log(db.collection("users").doc(uid))
});
Share Improve this question edited May 4, 2018 at 5:18 Dan McGrath 42k11 gold badges103 silver badges130 bronze badges asked Jan 31, 2018 at 5:33 Luuk LeijtensLuuk Leijtens 631 gold badge2 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

you can use firebase.auth().currentUser get current logged in user , checkout this code

btnName.addEventListener('click', e => {
    let user = firebase.auth().currentUser;    
    console.log(user);
    if (user) {
        console.log(db.collection("users").doc(user.uid))
    } else {
        alert('user not logged in')
    }
});
发布评论

评论列表(0)

  1. 暂无评论