Firestore 不将用户数据写入数据库
Firestore Not Writing User Data to Data Base
我正在尝试使用 FirebaseUI Web
和 Vue.js 在注册期间将用户数据加载到 Firestore 中。该帐户是在 Firebase 端创建的。但是,数据库未被填充 - 我没有收到任何错误,但 alert('Signed in as ' + displayName);
确实显示了正确的信息并且用户已登录。
更新:我通过在 callbacks
之外创建一个函数来重构代码并反映错误处理 - 问题没有变化(没有错误 && 没有写)。
firbaseui-config.js:
import firebase from 'firebase';
import firebaseui from 'firebaseui'
import { accountsCollection } from '@/services/registerFirebase'
export const FBuiConfig = {
signInSuccessUrl: '/dashboard',
signInFlow: 'popup',
callbacks: {
signInSuccessWithAuthResult: function(authResult) {
addUser(authResult);
return false;
},
uiShown: function() {
document.getElementById('app-drawer').style.display = 'none';
document.getElementById( 'app-toolbar').style.display = 'none';
},
},
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
{
provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
requireDisplayName: true,
forceSameDevice: true,
},
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
],
tosUrl: '/tos',
privacyPolicyUrl: function() {
window.location.assign('/pp');
}
};
function addUser(authResult) {
const user = authResult.user;
const userUid = user.uid;
const displayName = user.displayName;
const account = {
useruid: userUid,
calendarEvents: []
}
accountsCollection.doc(userUid).set(account)
.then(function (docRef) {
console.log('Document written with ID: ', docRef.id)
})
.catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
alert.error(
'Error adding document: ', errorCode + ': ' + errorMessage
)
})
alert('Signed in as ' + displayName); // <-- WORKS!
}
非常感谢@RenaudTarnec。数据是returning null
,不幸的是,Auth 提供商没有return 发生这种情况时出现任何错误。
通过使用需要的 returned 数据创建日志 before fb.usersCollection.doc(account.userUid).set(account)
帮助解决了这个问题。
Bellow 是最终的工作示例。
import firebase from 'firebase'
import firebaseui from 'firebaseui'
const fb = require('@/services/firebaseInit')
export var uiConfig = {
signInSuccessUrl: '/dashboard',
signInFlow: 'popup',
callbacks: {
signInSuccessWithAuthResult: function(authResult) {
const newUser = authResult.user
const account = {
avatar : newUser.photoURL,
eMail : newUser.email,
userName : newUser.displayName,
userUid : newUser.uid,
accountHolder: []
}
console.log(
'New user: '+ account.userName + '\n' +
'with id: ' + account.userUid + '\n' +
'email: ' + account.eMail + '\n' +
'avatar: ' + account.avatar
)
fb.usersCollection.doc(account.userUid).set(account)
.then(() => {
console.log('New user account with ID:' + account.userUid + ' created');
})
.catch(err => {
console.log(err)
this.errorMsg = err.message
})
alert('Hello ' + account.userName + ', welcome to Firestore!');
return false;
}
},
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
{
provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
requireDisplayName: true,
},
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
],
};
我正在尝试使用 FirebaseUI Web
和 Vue.js 在注册期间将用户数据加载到 Firestore 中。该帐户是在 Firebase 端创建的。但是,数据库未被填充 - 我没有收到任何错误,但 alert('Signed in as ' + displayName);
确实显示了正确的信息并且用户已登录。
更新:我通过在 callbacks
之外创建一个函数来重构代码并反映错误处理 - 问题没有变化(没有错误 && 没有写)。
firbaseui-config.js:
import firebase from 'firebase';
import firebaseui from 'firebaseui'
import { accountsCollection } from '@/services/registerFirebase'
export const FBuiConfig = {
signInSuccessUrl: '/dashboard',
signInFlow: 'popup',
callbacks: {
signInSuccessWithAuthResult: function(authResult) {
addUser(authResult);
return false;
},
uiShown: function() {
document.getElementById('app-drawer').style.display = 'none';
document.getElementById( 'app-toolbar').style.display = 'none';
},
},
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
{
provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
requireDisplayName: true,
forceSameDevice: true,
},
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
],
tosUrl: '/tos',
privacyPolicyUrl: function() {
window.location.assign('/pp');
}
};
function addUser(authResult) {
const user = authResult.user;
const userUid = user.uid;
const displayName = user.displayName;
const account = {
useruid: userUid,
calendarEvents: []
}
accountsCollection.doc(userUid).set(account)
.then(function (docRef) {
console.log('Document written with ID: ', docRef.id)
})
.catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
alert.error(
'Error adding document: ', errorCode + ': ' + errorMessage
)
})
alert('Signed in as ' + displayName); // <-- WORKS!
}
非常感谢@RenaudTarnec。数据是returning null
,不幸的是,Auth 提供商没有return 发生这种情况时出现任何错误。
通过使用需要的 returned 数据创建日志 before fb.usersCollection.doc(account.userUid).set(account)
帮助解决了这个问题。
Bellow 是最终的工作示例。
import firebase from 'firebase'
import firebaseui from 'firebaseui'
const fb = require('@/services/firebaseInit')
export var uiConfig = {
signInSuccessUrl: '/dashboard',
signInFlow: 'popup',
callbacks: {
signInSuccessWithAuthResult: function(authResult) {
const newUser = authResult.user
const account = {
avatar : newUser.photoURL,
eMail : newUser.email,
userName : newUser.displayName,
userUid : newUser.uid,
accountHolder: []
}
console.log(
'New user: '+ account.userName + '\n' +
'with id: ' + account.userUid + '\n' +
'email: ' + account.eMail + '\n' +
'avatar: ' + account.avatar
)
fb.usersCollection.doc(account.userUid).set(account)
.then(() => {
console.log('New user account with ID:' + account.userUid + ' created');
})
.catch(err => {
console.log(err)
this.errorMsg = err.message
})
alert('Hello ' + account.userName + ', welcome to Firestore!');
return false;
}
},
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
{
provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
requireDisplayName: true,
},
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
],
};