首页 > 解决方案 > 在firebase firestore云功能中获取当前用户的uid

问题描述

是否有可能在firebase firestore云功能中获取当前用户的uid。我尝试了一个示例代码,但它总是抛出一个错误;有没有办法实现这个功能?

这是我的代码:

  exports.test = functions.firestore.document('test/{doc}')
.onCreate(async (snap, context) => {
    const db = admin.auth();
    const uid = context.auth.uid;
    console.log(context.auth.uid, 'is the uid of cuurent user');
    console.log(db.getUser(uid), 'is the current user');
    return;
});

这段代码总是抛出错误;我不知道为什么?

 TypeError: Cannot read property 'uid' of undefined
at Object.<anonymous> (/user_code/lib/index.js:291:29)
at next (native)
at /user_code/lib/index.js:7:71
at __awaiter (/user_code/lib/index.js:3:12)
at exports.test.functions.firestore.document.onCreate (/user_code/lib/index.js:289:34)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:769:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7);

标签: google-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读