小编Aar*_*ron的帖子

admin.auth().currentUser; 在 Cloud Functions 中返回 undefined

我正在尝试创建一个函数,当设备在应用程序中注册时,会将这个设备 uid 附加到注册该设备的登录用户的 uid(这是在另一个 firestore 集合中,当用户寄存器)。

这是我的代码:

exports.addDeviceToUser = functions.firestore.document('device-names/{device}').onUpdate((change, context) => {
    const currentUser = admin.auth().currentUser;
    const deviceName = context.params.device;
    var usersRef = db.collection('users');
    var queryRef = usersRef.where('uid', '==', currentUser.uid);

    if (authVar.exists) {
        return queryRef.update({sensors: deviceName}).then((writeResult => {
        return console.log('Device attached');
        }));
    } else {return console.log('Device attachment failed, user not signed in');}
});
Run Code Online (Sandbox Code Playgroud)

我一直收到此错误:“类型错误:无法读取未定义的属性 'uid'。” 显然我无法访问当前用户的身份验证信息。为什么?

javascript firebase firebase-authentication google-cloud-functions google-cloud-firestore

0
推荐指数
1
解决办法
2100
查看次数