【发布时间】:2020-01-06 12:23:26
【问题描述】:
我对 firebase 和 javascript 非常陌生。
我的项目:构建一个私人消息应用程序。为此,我想在 firestore 中定义一个子集合,用于使用当前用户 ID 和目标用户 ID 进行私人消息传递。
这是允许这样做的函数:
// 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(),
});
};
【问题讨论】:
-
你可以使用异步等待!!
标签: javascript firebase google-cloud-firestore