首页 > 解决方案 > 是否可以使用 Cloud Functions 在 Firestore 中获取 userID 或 currentUser 子集合?

问题描述

我需要帮助才能使用 Cloud Functions(Admin SDK)在 Firestore 的子集合中获取 userId。我试着得到 ' ...must be a non-empty string 或 non-empty array。'

假设分贝const db = admin.firestore();

const snapshot = await db
            .collection('clients')
            .doc() // <-- If put a manually userId string, it works.
            .collection('tokens')
            .get();

const tokens = snapshot.docs.map(token => token.id);

编辑...

此函数从用户设备返回一个令牌。在.doc()中,如果我手动输入用户 ID,我可以获得记录在用户集合中的令牌。

.doc('oM5f5BcwJmA43EuuuElLAUUe7q12') // Example entering uid manually

部分代码:

export const sendToDevice = functions.firestore
    .document('/clients/{clientId}/{warnings}/{warningId}')
    .onCreate(async snapshot => {

        const warning = snapshot.data();

        const snapshot = await db
            .collection('clients')
            .doc()                 // <-- This can't be left blank, 
            .collection('tokens')  // otherwise this function 
            .get();                // generates a new documentID.


        const tokens = snapshot.docs.map(token => token.id);
...

数据库结构:

数据库结构

问候, 比尼奥

标签: firebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读