javascript - 在读取另一个函数之前等待 firebase.auth 初始化

标签 javascript firebase google-cloud-firestore

我对 firebase 和 javascript 非常陌生。

我的项目:构建一个私有(private)消息应用程序。为此,我想在 firestore 中定义一个子集合,用于使用当前用户 ID 和目标用户 ID 进行私有(private)消息传递。

这是允许这样做的函数:

// generate the right SubCollection depending on current User and the User he tries to reach
function dmCollection(toUid) {

  if (toUid === null) {
    // If no destination user is definer, we set it to the below value
    toUid = 'fixed_value';
  };
  const idPair = [firebase.auth().currentUser.uid, toUid].join('_').sort();
  return firebase.firestore().collection('dms').doc(idPair).collection('messages');
};

我的问题:我想使用 firebase.auth().currentUser.uid 属性,但该函数似乎没有等待 firebase .auth 初始化。我该如何解决这个问题?

其他信息: 我有两个函数正在调用第一个函数 (dmCollection):


// retrieve DMs
function messagesWith(uid) {
  return dmCollection(uid).orderBy('sent', 'desc').get();
};


// send a DM
function sendDM(toUid, messageText) {
  return dmCollection(toUid).add({
    from: firebase.auth().currentUser.uid,
    text: messageText,
    sent: firebase.firestore.FieldValue.serverTimestamp(),
  });
};

最佳答案

如果我正确理解您的问题(“看起来该函数没有等待 firebase.auth 初始化”),您有两种可能的解决方案:

解决方案 1:在 Auth 对象上设置观察者

documentation 中所述,您可以使用 onAuthStateChanged()Auth 对象上设置观察者方法:

By using an observer, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get the current user.

因此您可以按如下方式修改代码:

// retrieve DMs
function messagesWith(uid) {
  return dmCollection(uid).orderBy('sent', 'desc').get();
};


// send a DM
function sendDM(toUid, messageText) {
  return dmCollection(toUid).add({
    from: firebase.auth().currentUser.uid,
    text: messageText,
    sent: firebase.firestore.FieldValue.serverTimestamp(),
  });
};

// generate the right SubCollection depending on current User and the User he tries to reach
function dmCollection(toUid) {

  if (toUid === null) {
    // If no destination user is definer, we set it to the below value
    toUid = 'fixed_value';
  };
  const idPair = [firebase.auth().currentUser.uid, toUid].join('_').sort();
  return firebase.firestore().collection('dms').doc(idPair).collection('messages');
};


firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    var messageText = '....';
    sendDM(user.uid, messageText)
  } else {
    // No user is signed in.
  }
});
<小时/>

解决方案 2:使用 currentUser 属性

您还可以“使用 currentUser 属性获取当前登录的用户”,如同一 doc 中所述。 。 “如果用户未登录,currentUser 为 null”。

在这种情况下,你会这样做:

var user = firebase.auth().currentUser;

if (user) {
  var messageText = '....';
  sendDM(user.uid, messageText);
} else {
  // No user is signed in.
  // Ask the user to sign in, e.g. redirect to a sign in page
}
<小时/>

选择哪种解决方案?

这取决于您希望如何根据用户uid调用函数。

  • 如果您想在用户登录后立即调用函数,请使用解决方案 1。
  • 如果您想在其他特定时刻(例如,在用户操作之后)调用函数,请使用解决方案 2。

关于javascript - 在读取另一个函数之前等待 firebase.auth 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59612070/

相关文章:

javascript - Firestore 抛出 'onSnapshot() requires between 1 and 4 arguments'

javascript - Node.js - Express.js URL 参数验证

javascript - 如何选择数据切换折叠的父级?

javascript - 解析云代码新 SDK 包含子类不起作用

swift - FIRAuth 文件与 FirebaseUI 不兼容 : use of undeclared identifier 'FIRAuthErrorUserInfoUpdatedCredentialKey'

android - onDataChange 陷入无限循环

java - 使用 App Engine 自动缩放的 Firebase 身份验证

android - 无法从 Firestore 获取嵌套数据并将其显示在 Recyclerview 中

firebase - 检查firebase记录是否存在并返回 bool 值

javascript - 了解用户是否已经注册了 Reactjs 和 firebase-google auth