尝试在创建用户时更新实时数据库中的用户数据。这是我的代码:
const functions = require('firebase-functions');
const promise = require('request-promise');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var omitBy = require('lodash.omitby');
var isNil = require('lodash.isnil');
'use strict';
exports.userCreated = functions.auth.user().onCreate(event => {
let request = admin.auth().getUser(event.data.uid)
.then(function(user) {
console.log("Successfully fetched user data: ", user.toJSON());
var email, firstName, lastName, photoURL;
for (var provider of user.providerData) {
if (provider.email) {
email = provider.email;
}
if (provider.photoURL) {
photoURL = provider.photoURL;
}
if (provider.displayName) {
const names = provider.displayName.split(' ');
firstName = names[0];
if (names.length > 1) {
lastName = names[names.length - 1];
}
}
}
var values = omitBy({
email: email,
first_name: firstName,
last_name: lastName,
license_agreement_version: '1.1',
image_url: photoURL
}, isNil);
admin.database().ref('users/' + user.uid).set(values);
})
.catch(function(error) {
console.error("Error Fetching User: ", error);
});
return request;
});
但是,当通过创建用户时。 Facebook,提供商数据未提供。这是控制台日志:
Successfully fetched user data: { uid: 'exampleUID',
email: undefined,
emailVerified: false,
displayName: undefined,
photoURL: undefined,
disabled: false,
metadata:
{ lastSignedInAt: 2017-03-16T19:40:59.000Z,
createdAt: 2017-03-16T19:40:59.000Z },
providerData: [] }
我做错了什么,或者这些数据是否会在创作时提供?
答案 0 :(得分:2)
您无需再次拨打admin.auth().getUser(...)
。方便地,您在此函数中收到的event.data
已经是UserRecord
!
这是我打印Auth事件的非常简单的代码:
var functions = require('firebase-functions');
exports.helloAuth = functions.auth.user().onCreate(event => {
console.log("User created: " + JSON.stringify(event));
});
这是我通过Facebook登录时输出的内容:
{
"displayName": "Robert-Jan Huijsman",
"email": "REDACTED@gmail.com",
"metadata": {
"createdAt": "2017-03-17T01:34:03.000Z",
"lastSignedInAt": "2017-03-17T01:34:03.000Z"
},
"photoURL": "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/REDACTED",
"providerData": [
{
"displayName": "Robert-Jan Huijsman",
"email": "REDACTED@gmail.com",
"photoURL": "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/REDACTED",
"providerId": "facebook.com",
"uid": "http://facebook.com/1234567890"
}
],
"uid": "AaBbCcDdEeFf"
}