快照错误 firebase(读取 'onSnapshot')
snapchot error firebase (reading 'onSnapshot')
我有一个 firebase 问题,每当我尝试使用 firebase 进行身份验证时,我都会遇到这个问题
App.js:27 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'onSnapshot')
App.js :
{
const {setCurrentUser} = this.props
this.authListener = auth.onAuthStateChanged(async userAuth =>{
if(userAuth)
{
const userRef = await handleUserProfile(userAuth)
userRef.onSnapshot(snapchot=>{
setCurrentUser(
{
id:snapchot.id,
...snapchot.data(),
}
)
})
}
setCurrentUser(userAuth)
})
}
firebase.js :
export const handleUserProfile = async({ userAuth, additionalData }) => {
if (!userAuth) return;
const { uid } = userAuth;
const userRef = firestore.doc(`users/${uid}`);
const snapshot = await userRef.get();
if (!snapshot.exists) {
const { displayName, email } = userAuth;
const timestamp = new Date();
const userRoles = ['user'];
try {
await userRef.set({
displayName,
email,
createdDate: timestamp,
userRoles,
...additionalData
});
} catch (err) {
console.log(err);
}
}
return userRef;
};
我最终无法获得 currentUser 的 redux 状态,
我正在尝试在登录时保存 currentUser
:
const mapStatetoProps = ({user}) =>
({
currentUser:user.currentUser
})
const mapDispatchToProps = dispatch =>({
setCurrentUser:user => dispatch(setCurrentUser(user))
})
export default connect(mapStatetoProps,mapDispatchToProps)(App)
在这里,您正在从作为此函数中的参数接收的对象解构 userAuth
export const handleUserProfile = async({ userAuth, additionalData }) => {
在这里你直接传递了 userAuth:
const userRef = await handleUserProfile(userAuth)
相反,你应该在这里写:
const userRef = await handleUserProfile({userAuth})
这将在对象中传递 userAuth,因此解构将成功!
我有一个 firebase 问题,每当我尝试使用 firebase 进行身份验证时,我都会遇到这个问题
App.js:27 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'onSnapshot')
App.js :
{
const {setCurrentUser} = this.props
this.authListener = auth.onAuthStateChanged(async userAuth =>{
if(userAuth)
{
const userRef = await handleUserProfile(userAuth)
userRef.onSnapshot(snapchot=>{
setCurrentUser(
{
id:snapchot.id,
...snapchot.data(),
}
)
})
}
setCurrentUser(userAuth)
})
}
firebase.js :
export const handleUserProfile = async({ userAuth, additionalData }) => {
if (!userAuth) return;
const { uid } = userAuth;
const userRef = firestore.doc(`users/${uid}`);
const snapshot = await userRef.get();
if (!snapshot.exists) {
const { displayName, email } = userAuth;
const timestamp = new Date();
const userRoles = ['user'];
try {
await userRef.set({
displayName,
email,
createdDate: timestamp,
userRoles,
...additionalData
});
} catch (err) {
console.log(err);
}
}
return userRef;
};
我最终无法获得 currentUser 的 redux 状态, 我正在尝试在登录时保存 currentUser :
const mapStatetoProps = ({user}) =>
({
currentUser:user.currentUser
})
const mapDispatchToProps = dispatch =>({
setCurrentUser:user => dispatch(setCurrentUser(user))
})
export default connect(mapStatetoProps,mapDispatchToProps)(App)
在这里,您正在从作为此函数中的参数接收的对象解构 userAuth
export const handleUserProfile = async({ userAuth, additionalData }) => {
在这里你直接传递了 userAuth:
const userRef = await handleUserProfile(userAuth)
相反,你应该在这里写:
const userRef = await handleUserProfile({userAuth})
这将在对象中传递 userAuth,因此解构将成功!